<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Python基础 | Cquang博客</title><meta name="keywords" content="python,计算机科学课程"><meta name="author" content="Cai XianQuan"><meta name="copyright" content="Cai XianQuan"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="description" content="从本篇笔记开始利用周末时间学习计算机科学课程，预计需要两年时间，且行且珍惜，加油努力！">
<meta property="og:type" content="article">
<meta property="og:title" content="Python基础">
<meta property="og:url" content="https://xquan123.gitee.io/2020/07/19/Python%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="Cquang博客">
<meta property="og:description" content="从本篇笔记开始利用周末时间学习计算机科学课程，预计需要两年时间，且行且珍惜，加油努力！">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://xquan123.gitee.io/2020/07/19/Python%E5%9F%BA%E7%A1%80/Python%E5%9F%BA%E7%A1%80%E9%A6%96%E9%A1%B5.jpg">
<meta property="article:published_time" content="2020-07-19T03:30:06.000Z">
<meta property="article:modified_time" content="2020-12-05T16:34:59.000Z">
<meta property="article:author" content="Cai XianQuan">
<meta property="article:tag" content="python">
<meta property="article:tag" content="计算机科学课程">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://xquan123.gitee.io/2020/07/19/Python%E5%9F%BA%E7%A1%80/Python%E5%9F%BA%E7%A1%80%E9%A6%96%E9%A1%B5.jpg"><link rel="shortcut icon" href="/blog/img/favicon1.png"><link rel="canonical" href="https://xquan123.gitee.io/2020/07/19/Python%E5%9F%BA%E7%A1%80/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin="crossorigin"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web"><script>var GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  ClickShowText: undefined,
  lightbox: 'mediumZoom',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  justifiedGallery: {
    js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
    css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isanchor: false
};

var saveToLocal = {
  set: function setWithExpiry(key, value, ttl) {
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: true,
  isToc: true,
  postUpdate: '2020-12-06 00:34:59'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = 'false'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}})()</script><link rel="stylesheet" href="/css/VolantisTags.css"><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/blog/img/%E5%A4%B4%E5%83%8F.gif" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">22</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">33</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 享受时光</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/blog/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li></ul></div></div></div></div><div id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(/blog/2020/07/19/Python%E5%9F%BA%E7%A1%80/Python%E5%9F%BA%E7%A1%80%E9%A6%96%E9%A1%B5.jpg)"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">Cquang博客</a></span><span id="menus"><div id="search_button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 享受时光</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/blog/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li></ul></div></div><span class="close" id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></span></span></nav><div id="post-info"><h1 class="post-title">Python基础</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-07-19T03:30:06.000Z" title="发表于 2020-07-19 11:30:06">2020-07-19</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-12-05T16:34:59.000Z" title="更新于 2020-12-06 00:34:59">2020-12-06</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6%E8%AF%BE%E7%A8%8B/">计算机科学课程</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6%E8%AF%BE%E7%A8%8B/Prerequisites/">Prerequisites</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6%E8%AF%BE%E7%A8%8B/Prerequisites/Python%E5%9F%BA%E7%A1%80/">Python基础</a></span></div><div class="meta-secondline"> <span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">26.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>99分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><div class="note info flat"><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">--头部属性说明：</span><br><span class="line">title	【必需】文章标题【需要】</span><br><span class="line">date	【必需】文章创建日期【需要】</span><br><span class="line">tags	【可选】文章标签【需要】</span><br><span class="line">categories	【可选】文章分类【需要】</span><br><span class="line">keywords	【可选】文章关键字【需要，同标签】</span><br><span class="line">description	【可选】文章描述【需要】</span><br><span class="line">top_img	【可选】文章顶部图片</span><br><span class="line">cover	【可选】文章缩略图(如果没有设置top_img,文章页顶部将显示缩略图，可设为false/图片地址/留空)【需要，地址为：/年/月/日/文章标题/图片名称.后缀名】</span><br><span class="line">comments	【可选】显示文章评论模块(默认 true)</span><br><span class="line">toc	【可选】显示文章TOC(默认为设置中toc的enable配置)</span><br><span class="line">toc_number	【可选】显示</span><br><span class="line">toc_number	(默认为设置中toc的number配置)</span><br><span class="line">copyright	【可选】显示文章版权模块(默认 true)</span><br><span class="line">mathjax	【可选】显示mathjax(当设置mathjax的per_page: false时，才需要配置，默认 false)</span><br><span class="line">katex	【可选】显示katex(当设置katex的per_page: false时，才需要配置，默认 false)</span><br><span class="line"></span><br><span class="line">--标签外挂</span><br><span class="line">样式：</span><br><span class="line">[class] : default | primary | success | info | warning | danger.</span><br><span class="line">&#123;% note info %&#125;</span><br><span class="line">编辑内容</span><br><span class="line">&#123;% endnote %&#125;</span><br><span class="line"></span><br><span class="line">--图片插入示例：</span><br><span class="line">不显示描述，可以插入【舍弃不用】：</span><br><span class="line">&#123;% asset_img example.png %&#125;</span><br><span class="line">显示描述的：</span><br><span class="line">![example](example.png)	不用添加路径，直接填图片名称即可，将图片放入对应文件夹内</span><br><span class="line"></span><br><span class="line">-- 插入链接</span><br><span class="line">&#123;% link text url [external] [title] %&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>
<h1 id="第一章-前言"><a href="#第一章-前言" class="headerlink" title="第一章 前言"></a>第一章 前言</h1><p>主要是参考尚硅谷的李立超老师的Python基础视频笔记，加上自己的所总结的知识点，形成比较完善的python笔记，方便自己后续查阅已经复习巩固资料。</p>
<h1 id="第二章-计算机基础知识"><a href="#第二章-计算机基础知识" class="headerlink" title="第二章 计算机基础知识"></a>第二章 计算机基础知识</h1><div class="note default flat"><p>该章比较基础，可有可无，当做扩展资料。</p>
</div>
<h2 id="计算机是什么"><a href="#计算机是什么" class="headerlink" title="计算机是什么"></a>计算机是什么</h2><p>在现实生活中，越来越无法离开计算机了<br>  电脑、笔记本、手机、游戏机、汽车导航、智能电视 。。。<br>计算机就是一个用来计算的机器！<br>目前来讲，计算机只能根据人类的指令来完成各种操作，人让它干嘛他就得干嘛<br>所以我们学习计算机，就是学习如何控制计算机！</p>
<h2 id="计算机的组成"><a href="#计算机的组成" class="headerlink" title="计算机的组成"></a>计算机的组成</h2><p>计算机由两部分组成：硬件 和 软件<br>硬件包含：键盘、鼠标、显示器、CPU、主板、内存、硬盘 。。。<br>   硬件是看的见摸得着的<br>  软件包含：系统软件（windows、macOS、Linux）和应用软件（office 、QQ、绝地求生）<br>   软件是看的见摸不着的，软件负责控制计算机中的硬件    </p>
<h2 id="计算机的使用方式"><a href="#计算机的使用方式" class="headerlink" title="计算机的使用方式"></a>计算机的使用方式</h2><p>我们必须要通过软件来对计算机完成各种操作，<br>   但是注意，软件中并不是所有的功能都会对用户开放，<br>   用户需要调用软件提供的接口（Interface 交互界面）来操作计算机</p>
<p>用户界面分成两种：TUI（文本交互界面）和 GUI（图形化交互界面）</p>
<h2 id="windows的命令行"><a href="#windows的命令行" class="headerlink" title="windows的命令行"></a>windows的命令行</h2><p>命令行就是文本交互界面，通过命令行可以使用一个一个的指令来操作计算机<br>任何的计算机的操作系统中都包含有命令行（windows、linux、macOS）<br>命令行有多个不同的名字：<br>   命令行、命令行窗口、DOS窗口、命令提示符、CMD窗口、Shell、终端、Terminal<br>1.如何进入到命令行<br>   win键 + R 出现运行窗口，输入cmd，然后回车<br>2.命令行的结构</p>
<ul>
<li><p>版本及版权声明（一般没有什么用）<br>Microsoft Windows [版本 10.0.16299.431]<br>(c) 2017 Microsoft Corporation。保留所有权利。</p>
</li>
<li><p>命令提示符<br> C:\Users\lilichao&gt;<br> C:  </p>
</li>
<li><p>当前所在的磁盘根目录</p>
</li>
<li><p>可以通过 x: 来切换盘符（x表示你的盘符）</p>
<p> \Users\lilichao</p>
<ul>
<li>所在磁盘的路径，当前所在的文件夹</li>
<li>cd 来切换目录<br>&gt;</li>
<li>命令提示符，在大于号后边可以直接输入指令</li>
</ul>
</li>
</ul>
<p>3.常用的dos命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">      dir 查看当前目录下的所有文件（夹）</span><br><span class="line">      cd 进入到指定的目录</span><br><span class="line">          . 表示当前目录</span><br><span class="line">          .. 表示上一级目录</span><br><span class="line">      md 创建一个目录</span><br><span class="line">      rd 删除一个目录    </span><br><span class="line">      del 删除一个文件</span><br><span class="line">      cls 清除屏幕</span><br><span class="line">  </span><br><span class="line">命令的语法</span><br><span class="line"> 			命令 [参数] [选项]</span><br></pre></td></tr></table></figure>
<p>4.小技巧</p>
<ul>
<li>方向键上下，查看命令的历史记录</li>
<li>tab键自动补全命令        </li>
</ul>
<h2 id="环境变量（environment-variable）"><a href="#环境变量（environment-variable）" class="headerlink" title="环境变量（environment variable）"></a>环境变量（environment variable）</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">环境变量指的就是操作系统当中的一些变量。</span><br><span class="line">可以通过修改环境变量，来对计算机进行配置（主要是来配置一些路径的）</span><br><span class="line">1.查看环境变量</span><br><span class="line">    &gt; 右键 计算机（此电脑），选择属性</span><br><span class="line">    &gt; 系统界面左侧选择 高级系统设置</span><br><span class="line">    &gt; 选择环境变量</span><br><span class="line">    环境变量界面分成了两个部分，上边是用户环境变量，下边是系统环境变量</span><br><span class="line">        建议只修改用户的环境变量，不要修改系统的环境变量</span><br><span class="line">        &#123;% note info %&#125;</span><br><span class="line">        必要时还是需要修改系统变量，具体问题具体分析</span><br><span class="line">        &#123;% endnote&#125;</span><br><span class="line"></span><br><span class="line">2.添加环境变量</span><br><span class="line">    &gt; 通过新建按钮添加环境变量</span><br><span class="line">    &gt; 一个环境变量可以由多个值，值与值之间使用;（英文）隔开</span><br><span class="line"></span><br><span class="line">3.修改环境变量</span><br><span class="line">    &gt; 通过编辑按钮来修改环境变量</span><br><span class="line"></span><br><span class="line">4.删除环境变量</span><br><span class="line">    &gt; 通过删除按钮来删除环境变量    </span><br></pre></td></tr></table></figure>
<h2 id="path环境变量"><a href="#path环境变量" class="headerlink" title="path环境变量"></a>path环境变量</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">path环境变量中保存的是一个一个的路径。</span><br><span class="line">当我们在命令行中输入一个命令（或访问一个文件时），</span><br><span class="line">    系统会首先在当前目录下寻找，如果找到了则直接执行或打开</span><br><span class="line">    如果没有找到，则会依次去path环境变量的路径中去寻找，直到找到为止</span><br><span class="line">    如果path环境变量中的路径都没有找到，则报错</span><br><span class="line">        &#39;xxx&#39; 不是内部或外部命令，也不是可运行的程序或批处理文件。</span><br><span class="line">我们可以将一些经常需要访问到的文件会程序的路径，添加到path环境变量中，</span><br><span class="line">    这样我们就可以在任意的位置访问到这些文件了</span><br><span class="line"></span><br><span class="line">注意事项：</span><br><span class="line">    1.如果环境变量中没有path，可以手动添加</span><br><span class="line">    2.path环境变量不区分大小写 PATH Path path   </span><br><span class="line">    3.修改完环境变量必须重新启动命令行窗口</span><br><span class="line">    4.多个路径之间使用;隔开</span><br></pre></td></tr></table></figure>
<h2 id="进制"><a href="#进制" class="headerlink" title="进制"></a>进制</h2><div class="note primary flat"><p>B：二进制<br>O：八进制<br>D：十进制<br>H：十六进制</p>
</div>

<ul>
<li><p>十进制（最常用的进制）</p>
<ul>
<li>十进制就是满十进一的进制</li>
<li>十进制当中一共有10个数字<pre><code>  0 1 2 3 4 5 6 7 8 9
</code></pre>
</li>
<li>十进制如何计数<pre><code>  0 1 2 3 4 5 6 7 8 9 10 11 12 。。。 19 20 。。。29 30
</code></pre>
</li>
</ul>
</li>
<li><p>二进制（计算机底层使用的进制）</p>
<ul>
<li>满二进一</li>
<li>二进制中一共有2个数字<pre><code>0 1
</code></pre>
</li>
<li>二进制如何计数<pre><code>0 1 10 11 100 101 110 111 1000
</code></pre>
</li>
<li>所有的数据在计算机底层都是以二进制的形式保存的，计算机只认二进制</li>
<li>可以将内存想象为一个一个的小格子，小格子中可以存储一个0或一个1</li>
<li>内存中的每一个小格子，我们称为1bit（位）<br>   bit是计算机中的最小的单位<br>   byte是我们最小的可操作的单位<br>   8bit = 1byte（字节）<br>   1024byte = 1kb（千字节）<br>   1024kb = 1mb（兆字节）<br>   1024mb = 1gb（吉字节）<br>   1024gb = 1tb（太字节）<br>   。。。</li>
</ul>
</li>
</ul>
<ul>
<li>八进制（一般不用）<ul>
<li>满八进一</li>
<li>八进制中一共有8个数字<br>  0 1 2 3 4 5 6 7</li>
<li>八进制如何计数<br>  0 1 2 3 4 5 6 7 10 11 。。。 17 20 。。。27 30</li>
</ul>
</li>
<li>十六进制<ul>
<li>满十六进一</li>
<li>十六进制中一共有16个数字<br>   由于十六进制是满16才进位，<br>   所以十六进制中引入了a b c d e f来表示 10 11 12 13 14 15   <pre><code>0 1 2 3 4 5 6 7 8 9 a b c d e f 
</code></pre>
</li>
<li>十六进制如何计数<br>  0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 。。。 1a 1b 1c 1d 1e 1f<pre><code>  20 21 22 。。。 2a 2b 2c 2d 2e 2f 30    
</code></pre>
</li>
<li>我们在查看二进制数据时，一般会以十六进制的形式显示</li>
</ul>
</li>
</ul>
<h2 id="进制转换"><a href="#进制转换" class="headerlink" title="进制转换"></a>进制转换</h2><h3 id="二进制数转换成十进制数"><a href="#二进制数转换成十进制数" class="headerlink" title="二进制数转换成十进制数"></a>二进制数转换成十进制数</h3><p>   由二进制数转换成十进制数的基本做法是，把二进制数首先写成加权系数展开式，然后按十进制加法规则求和。这种做法称为”按权相加”法。<br>   例1105 把二进制数110.11转换成十进制数。<br>   <img src="1%E3%80%81%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2%E5%8D%81%E8%BF%9B%E5%88%B6%E4%BE%8B%E5%AD%90.png" alt="二进制转换十进制例子"></p>
<h3 id="、十进制数转换为二进制数"><a href="#、十进制数转换为二进制数" class="headerlink" title="、十进制数转换为二进制数"></a>、十进制数转换为二进制数</h3><p>十进制数转换为二进制数时，由于整数和小数的转换方法不同，所以先将十进制数的整数部分和小数部分分别转换后，再加以合并。
    </p>
<ul>
<li><p>十进制整数转换为二进制整数<br>    十进制整数转换为二进制整数采用”<font color=red size=3><code>除2取余，逆序排列</code></font>“法。具体做法是：用2去除十进制整数，可以得到一个商和余数；再用2去除商，又会得到一个商和余数，如此进行，直到商为零时为止，然后把先得到的余数作为二进制数的低位有效位，后得到的余数作为二进制数的高位有效位，依次排列起来。<br>    例1107 把 (173)10 转换为二进制数。<br><img src="2%E3%80%81%E5%8D%81%E8%BF%9B%E5%88%B6%E6%95%B4%E6%95%B0%E8%BD%AC%E6%8D%A2%E4%BA%8C%E8%BF%9B%E5%88%B6.png" alt="十进制整数转换二进制"></p>
</li>
<li><p>十进制小数转换为二进制小数<br>    十进制小数转换成二进制小数采用”乘2取整，顺序排列”法。具体做法是：用2乘十进制小数，可以得到积，将积的整数部分取出，再用2乘余下的小数 部分，又得到一个积，再将积的整数部分取出，如此进行，直到积中的小数部分为零，或者达到所要求的精度为止。<br>    然后把取出的整数部分按顺序排列起来，先取的整数作为二进制小数的高位有效位，后取的整数作为低位有效位。<br>    【例1108】把（0.8125）转换为二进制小数。<br><img src="3%E3%80%81%E5%8D%81%E8%BF%9B%E5%88%B6%E5%B0%8F%E6%95%B0%E8%BD%AC%E6%8D%A2%E4%BA%8C%E8%BF%9B%E5%88%B6.png" alt="十进制小数转换二进制"></p>
</li>
</ul>
<font color=red size=3>
  例1109 （173.8125）10＝（ ）2
    解： 由［例1107］得（173）10＝（10101101）2
由［例1108］得（0.8125）10＝（0.1101）2
把整数部分和小数部分合并得： （173.8125）10＝（10101101.1101）2
</font>

<p>十进制小数转换成二进制小数采用”乘2取整，顺序排列”法。具体做法是：用2乘十进制小数，可以得到积，将积的整数部分取出，再用2乘余下的小数部分，又 得到一个积，再将积的整数部分取出，如此进行，直到积中的整数部分为零，或者整数部分为1，此时0或1为二进制的最后一位。或者达到所要求的精度为止。<br>然后把取出的整数部分按顺序排列起来，先取的整数作为二进制小数的高位有效位，后取的整数作为低位有效位。<br>十进制小数转二进制</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">如：0.625=（0.101）B</span><br><span class="line">0.625*2=1.25======取出整数部分1</span><br><span class="line">0.25*2=0.5========取出整数部分0</span><br><span class="line">0.5*2=1==========取出整数部分1</span><br><span class="line"></span><br><span class="line">再如：0.7=（0.1 0110 0110...）B</span><br><span class="line">0.7*2=1.4========取出整数部分1</span><br><span class="line">0.4*2=0.8========取出整数部分0</span><br><span class="line">0.8*2=1.6========取出整数部分1</span><br><span class="line">0.6*2=1.2========取出整数部分1</span><br><span class="line">0.2*2=0.4========取出整数部分0</span><br><span class="line">0.4*2=0.8========取出整数部分0</span><br><span class="line">0.8*2=1.6========取出整数部分1</span><br><span class="line">0.6*2=1.2========取出整数部分1</span><br><span class="line">0.2*2=0.4========取出整数部分0</span><br></pre></td></tr></table></figure>
<h2 id="文本文件和字符集"><a href="#文本文件和字符集" class="headerlink" title="文本文件和字符集"></a>文本文件和字符集</h2><ol>
<li>文本分成两种，一种叫做纯文本，还有一种叫做富文本</li>
<li>纯文本中只能保存单一的文本内容，无法保存内容无关的东西（字体、颜色、图片。。。）</li>
<li>富文本中可以保存文本以外的内容（word文档）</li>
<li>在开发时，编写程序使用的全都是纯文本！</li>
<li>纯文本在计算机底层也会转换为二进制保存，</li>
</ol>
<ul>
<li>将字符转换为二进制码的过程，我们称为编码</li>
<li>将二进制码转换为字符的过程，我们称为解码</li>
<li>编码和解码时所采用的规则，我们称为字符集</li>
</ul>
<ol start="6">
<li>常见的字符集：</li>
</ol>
<ul>
<li>ASCII<ul>
<li>美国人编码，使用7位来对美国常用的字符进行编码</li>
<li>包含128个字符</li>
</ul>
</li>
<li>ISO-8859-1<ul>
<li>欧洲的编码，使用8位</li>
<li>包含256个字符</li>
</ul>
</li>
<li>GB2312</li>
<li>GBK<ul>
<li>国标码，中国的编码</li>
</ul>
</li>
<li>Unicode   <ul>
<li>万国码，包含世界上所有的语言和符号，编写程序时一般都会使用Unicode编码</li>
<li>Unicode编码有多种实现，UTF-8 UTF-16 UTF-32</li>
<li>最常用的就是UTF-8</li>
</ul>
</li>
</ul>
<ol start="7">
<li>乱码</li>
</ol>
<ul>
<li>编写程序时，如果发现程序代码出现乱码的情况，就要马上去检查字符集是否正确     </li>
</ul>
<h2 id="Sublime-Text-3"><a href="#Sublime-Text-3" class="headerlink" title="Sublime Text 3"></a>Sublime Text 3</h2><ul>
<li>纯文本编辑器</li>
<li>Package Control（Sublime的包管理器）<ul>
<li>Sublime Text3中的插件，通过该插件可以向Sublime中安装新的包 </li>
</ul>
</li>
</ul>
<h1 id="第三章-Python入门"><a href="#第三章-Python入门" class="headerlink" title="第三章 Python入门"></a>第三章 Python入门</h1><h2 id="什么是计算机语言"><a href="#什么是计算机语言" class="headerlink" title="什么是计算机语言"></a>什么是计算机语言</h2><p>计算机就是一台用来计算机的机器，人让计算机干什么计算机就得干什么！<br>需要通过计算机的语言来控制计算机（编程语言）！<br>计算机语言其实和人类的语言没有本质的区别，不同点就是交流的主体不同！<br>计算机语言发展经历了三个阶段：</p>
<ol>
<li>机器语言</li>
</ol>
<ul>
<li>机器语言通过二进制编码来编写程序</li>
<li>执行效率好，编写起来太麻烦</li>
</ul>
<ol start="2">
<li>符号语言（汇编）</li>
</ol>
<ul>
<li>使用符号来代替机器码</li>
<li>编写程序时，不需要使用二进制，而是直接编写符号</li>
<li>编写完成后，需要将符号转换为机器码，然后再由计算机执行</li>
<li>符号转换为机器码的过程称为汇编</li>
<li>将机器码转换为符号的过程，称为反汇编  </li>
<li>汇编语言一般只适用于某些硬件，兼容性比较差  </li>
</ul>
<ol start="3">
<li>高级语言</li>
</ol>
<ul>
<li>高级语言的语法基本和现在英语语法类似，并且和硬件的关系没有那么紧密了</li>
<li>也就是说我们通过高级语言开发程序可以在不同的硬件系统中执行</li>
<li>并且高级语言学习起来也更加的容易，现在我们知道的语言基本都是高级语言</li>
<li>C、C++、C#、Java、JavaScript、Python 。。。</li>
</ul>
<h2 id="编译型语言和解释型语言"><a href="#编译型语言和解释型语言" class="headerlink" title="编译型语言和解释型语言"></a>编译型语言和解释型语言</h2><p>计算机只能识别二进制编码（机器码），所以任何的语言在交由计算机执行时必须要先转换为机器码，<br>也就是像 print(‘hello’) 必需要转换为类似 1010101 这样的机器码   </p>
<p>根据转换时机的不同，语言分成了两大类：</p>
<ol>
<li>编译型语言</li>
</ol>
<ul>
<li>C语言</li>
<li>编译型语言，会在代码执行前将代码编译为机器码，然后将机器码交由计算机执行</li>
<li>a(源码) –编译–&gt; b(编译后的机器码)</li>
<li>特点：<ul>
<li>执行速度特别快</li>
<li>跨平台性比较差</li>
</ul>
</li>
</ul>
<ol start="2">
<li>解释型语言 </li>
</ol>
<ul>
<li>Python JS Java</li>
<li>解释型语言，不会在执行前对代码进行编译，而是在执行的同时一边执行一边编译</li>
<li>a（源码）–解释器–&gt; 解释执行  </li>
<li>特点：<ul>
<li>执行速度比较慢</li>
<li>跨平台性比较好   </li>
</ul>
</li>
</ul>
<h2 id="Python的介绍"><a href="#Python的介绍" class="headerlink" title="Python的介绍"></a>Python的介绍</h2><ol>
<li><p>Python是解释型语言</p>
</li>
<li><p>Python（英国发音：/?pa?θ?n/ 美国发音：/?pa?θɑ?n/），是一种广泛使用的高级编程语言，属于通用型编程语言，由吉多·范罗苏姆创造，第一版发布于1991年。可以视之为一种改良（加入一些其他编程语言的优点，如面向对象）的LISP。作为一种解释型语言，Python的设计哲学强调代码的可读性和简洁的语法（尤其是使用空格缩进划分代码块，而非使用大括号或者关键词）。相比于C++或Java，Python让开发者能够用更少的代码表达想法。不管是小型还是大型程序，该语言都试图让程序的结构清晰明了。 </p>
</li>
<li><p>Life is short you need Python （人生苦短，我用Python）    </p>
</li>
<li><p>Python的用途：</p>
</li>
</ol>
<ul>
<li>WEB应用: Facebook 豆瓣 。。。</li>
<li>爬虫程序</li>
<li>科学计算</li>
<li>自动化运维</li>
<li>大数据（数据清洗）</li>
<li>云计算</li>
<li>桌面软件/游戏</li>
<li>人工智能</li>
<li>。。。     </li>
</ul>
<h2 id="Python开发环境搭建"><a href="#Python开发环境搭建" class="headerlink" title="Python开发环境搭建"></a>Python开发环境搭建</h2><ol>
<li>开发环境搭建就是安装Python的解释器</li>
<li>Python的解释器分类：</li>
</ol>
<ul>
<li>CPython（官方）：用c语言编写的Python解释器</li>
<li>PyPy：用Python语言编写的Python解释器</li>
<li>IronPython：用.net编写的Python解释器</li>
<li>Jython：用Java编写的Python解释器</li>
</ul>
<ol start="3">
<li>步骤：</li>
</ol>
<ul>
<li>下载安装包 python-3.6.5.exe<ul>
<li>3.x</li>
<li>2.x    </li>
</ul>
</li>
<li>安装（傻瓜式安装） </li>
<li>打开命令行窗口，输入python 出现如下内容<div class="note primary flat"><p>Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32<br>Type “help”, “copyright”, “credits” or “license” for more information.<br>&gt;&gt;&gt;    </p>
</div>

</li>
</ul>
<h2 id="Python的交互界面"><a href="#Python的交互界面" class="headerlink" title="Python的交互界面"></a>Python的交互界面</h2><ol>
<li>当我们通过命令行来输入Python，所进入到的界面就是Python的交互界面<br>结构：</li>
</ol>
<ul>
<li><p>版本和版权声明：</p>
<ul>
<li>Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32</li>
<li>Type “help”, “copyright”, “credits” or “license” for more information.</li>
</ul>
</li>
<li><p>命令提示符：</p>
<ul>
<li>&gt;&gt;&gt;</li>
</ul>
</li>
</ul>
<ol start="2">
<li><p>在命令提示符后可以直接输入Python的指令！输入完的指令将会被Python的解释器立即执行！</p>
</li>
<li><p>安装Python的同时，会自动安装一个Python的开发工具IDLE，通过IDLE也可以进入到交互模式<br>但是不同的是，在IDLE中可以通过TAB键来查看语句的提示。<br>IDLE实际上就是一个交互界面，但是他可以有一些简单的提示，并且可以将代码保存</p>
</li>
<li><p>交互模式只能你输入一行代码，它就是执行一行，所以他并不适用于我们日常的开发！ </p>
</li>
</ol>
<ul>
<li>仅可以用来做一些日常的简单的测试！   </li>
</ul>
<ol start="5">
<li><p>我们一般会将Python代码编写到一个py文件中，然后通过python指令来执行文件中的代码</p>
</li>
<li><p>练习：</p>
</li>
</ol>
<ul>
<li>自己尝试创建一个py文件，并向文件中写入python打印语句（print…） </li>
<li>然后执行该文件。</li>
<li>如果你的系统的扩展名无法修改，请尝试自行baidu！</li>
</ul>
<h2 id="Python和Sublime的整合"><a href="#Python和Sublime的整合" class="headerlink" title="Python和Sublime的整合"></a>Python和Sublime的整合</h2><p>1.在Sublime中执行Python代码，ctrl + b 自动在Sublime内置的控制台中执行<br>    这种执行方式，在某些版本的Sublime中对中文支持不好，并且不能使用input()函数</p>
<p>2.使用SublimeREPL来运行python代码<br>    安装完成，设置快捷键，希望按f5则自动执行当前的Python代码<br>    { “keys”: [“f5”], “caption”: “SublimeREPL:Python”,”command”: “run_existing_window_command”, “args”:{“id”: “repl_python_run”,”file”: “config/Python/Main.sublime-menu”}},</p>
<h2 id="基本语法"><a href="#基本语法" class="headerlink" title="基本语法"></a>基本语法</h2><ol>
<li>在Python中严格区分大小写</li>
<li>Python中的每一行就是一条语句，每条语句以换行结束</li>
<li>Python中每一行语句不要过长（规范中建议每行不要超过80个字符）</li>
</ol>
<ul>
<li>“rulers”:[80],</li>
</ul>
<ol start="4">
<li>一条语句可以分多行编写，多行编写时语句后边以\结尾  </li>
<li>Python是缩进严格的语言，所以在Python中不要随便写缩进  </li>
<li>在Python中使用#来表示注释，#后的内容都属于注释，注释的内容将会被解释器所忽略</li>
</ol>
<ul>
<li>我们可以通过注释来对程序进行解释说明，一定要养成良好的编写注释的习惯</li>
<li>注释要求简单明了，一般习惯上#后边会跟着一个空格</li>
</ul>
<ol start="7">
<li>在使用变量之前，需要对其先赋值</li>
<li>变量名可以包括字母、数字、下划线，但变量名不能以数字开头</li>
</ol>
<h2 id="常用操作符"><a href="#常用操作符" class="headerlink" title="常用操作符"></a>常用操作符</h2><h3 id="除法"><a href="#除法" class="headerlink" title="除法"></a>除法</h3><p>使用<code>/</code>精确除；<br>使用 <code>//</code>地板除法，即省略小数点后位数，保留整数 （向下取整）</p>
<h3 id="幂运算："><a href="#幂运算：" class="headerlink" title="幂运算：**"></a>幂运算：**</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">3</span> ** <span class="number">2</span>  = <span class="number">9</span>;</span><br><span class="line"><span class="number">5</span> ** <span class="number">3</span> = <span class="number">125</span>;</span><br></pre></td></tr></table></figure>
<h3 id="not-逻辑运算符"><a href="#not-逻辑运算符" class="headerlink" title="not 逻辑运算符"></a>not 逻辑运算符</h3><p>取相反<br><img src="7%E3%80%81%E4%BC%98%E5%85%88%E7%BA%A7%E9%97%AE%E9%A2%98.png" alt="优先级问题"></p>
<h3 id="一些注意点"><a href="#一些注意点" class="headerlink" title="一些注意点"></a>一些注意点</h3><h3 id="使用反斜杠"><a href="#使用反斜杠" class="headerlink" title="使用反斜杠"></a>使用反斜杠</h3><p>在路径字符串使用反斜杠，需要使用转义字符<code>\</code>，如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">str</span> = <span class="string">&#x27;C:\\row&#x27;</span> ——&gt; print(<span class="built_in">str</span>) ——&gt; C:\row</span><br><span class="line"><span class="built_in">str</span> = <span class="string">r&#x27;C:\row&#x27;</span> ——&gt; print(<span class="built_in">str</span>) ——&gt; C:\row   \\使用r来添加转义字符，只对反斜杠转义生效</span><br><span class="line"></span><br><span class="line">\\如果路径末尾加上反斜杠，则不能使用r来自动添加反斜杠，可以如下使用</span><br><span class="line"><span class="built_in">str</span> = <span class="string">r&#x27;C:\row&#x27;</span> + <span class="string">&#x27;\\&#x27;</span></span><br></pre></td></tr></table></figure>
<h3 id="三重引号"><a href="#三重引号" class="headerlink" title="三重引号"></a>三重引号</h3><p>得到一个跨越多行的字符串，使用三重引号字符串，例如：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">str</span> = <span class="string">&#x27;&#x27;&#x27;我爱鱼C，</span></span><br><span class="line"><span class="string">正如我爱小甲鱼，</span></span><br><span class="line"><span class="string">他那噶几噶几</span></span><br><span class="line"><span class="string">呱唧呱唧</span></span><br><span class="line"><span class="string">呱唧呱唧的声音，</span></span><br><span class="line"><span class="string">总缠绕于我的脑海，</span></span><br><span class="line"><span class="string">久久不肯散去。。。。。</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
<h2 id="对象（object）"><a href="#对象（object）" class="headerlink" title="对象（object）"></a>对象（object）</h2><ul>
<li>Python是一门面向对象的语言</li>
<li>一切皆对象！</li>
<li>程序运行当中，所有的数据都是存储到内存当中然后再运行的！</li>
<li>对象就是内存中专门用来存储指定数据的一块区域</li>
<li>对象实际上就是一个容器，专门用来存储数据</li>
<li>像我们之前学习的数值、字符串、布尔值、None都是对象<br><img src="4%E3%80%81%E4%B8%80%E5%88%87%E7%9A%86%E5%AF%B9%E8%B1%A1.png" alt="一切皆对象"></li>
</ul>
<h2 id="对象的结构"><a href="#对象的结构" class="headerlink" title="对象的结构"></a>对象的结构</h2><ul>
<li><p>每个对象中都要保存三种数据</p>
</li>
<li><p>id（标识）</p>
<blockquote>
<p>id用来标识对象的唯一性，每一个对象都有唯一的id<br>对象的id就相当于人的身份证号一样<br>可以通过id()函数来查看对象的id<br>id是由解析器生成的，在CPython中，id就是对象的内存地址<br>对象一旦创建，则它的id永远不能再改变</p>
</blockquote>
</li>
<li><p>type（类型）</p>
<blockquote>
<p>类型用来标识当前对象所属的类型<br>比如：int str float bool 。。。<br>类型决定了对象有哪些功能<br>通过type()函数来查看对象的类型<br>Python是一门强类型的语言，对象一旦创建类型便不能修改</p>
</blockquote>
</li>
</ul>
<ul>
<li>value（值）<blockquote>
<p>值就是对象中存储的具体的数据<br>对于有些对象值是可以改变的<br>对象分成两大类，可变对象 不可变对象</p>
<pre><code>  可变对象的值可以改变
  不可变对象的值不能改变，之前学习的对象都是不可变对象
</code></pre>
</blockquote>
</li>
</ul>
<p><img src="5%E3%80%81%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%BB%93%E6%9E%84.png" alt="对象的结构"></p>
<h2 id="可变对象与不可变对象"><a href="#可变对象与不可变对象" class="headerlink" title="可变对象与不可变对象"></a>可变对象与不可变对象</h2><ol>
<li><p>Python 在 heap 中分配的对象分成两类：可变对象和不可变对象。所谓可变对象是指，对象的内容是可变的，例如 list。而不可变的对象则相反，表示其内容不可变。</p>
<div class="note primary flat"><p>不可变对象 ：int，string，float，tuple   -– 可理解为C中，该参数为值传递<br>可变对象   ：list，dictionary，set           -– 可理解为C中，该参数为指针传递</p>
</div>
</li>
<li><p>由于 Python 中的变量存放的是对象引用，所以对于不可变对象而言，尽管对象本身不可变，但变量的对象引用是可变的。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#因为258是int对象，是不可变对象的。所以下面3个id的值都是一样的，最后一句的结果也是为True</span></span><br><span class="line"><span class="comment">#该代码最好在IDE工具下运行，在命令行会有不同结果</span></span><br><span class="line">print(<span class="built_in">id</span>(<span class="number">258</span>))</span><br><span class="line">a = <span class="number">258</span></span><br><span class="line">print(<span class="built_in">id</span>(a))</span><br><span class="line">b = <span class="number">258</span></span><br><span class="line">print(<span class="built_in">id</span>(b))</span><br><span class="line">print(a <span class="keyword">is</span> b)</span><br></pre></td></tr></table></figure>
<p><img src="10%E3%80%81%E4%B8%8D%E5%8F%AF%E5%8F%98%E5%AF%B9%E8%B1%A1.png" alt="不可变对象"></p>
</li>
<li><p>可变对象：其对象的内容是可以变化的。当对象的内容发生变化时，变量的对象引用是不会变化的。<br><img src="11%E3%80%81%E5%8F%AF%E5%8F%98%E5%AF%B9%E8%B1%A1.png" alt="可变对象"></p>
</li>
</ol>
<h3 id="可变对象"><a href="#可变对象" class="headerlink" title="可变对象"></a>可变对象</h3><ul>
<li><p>每个对象中都保存了三个数据：</p>
<ul>
<li>id（标识）</li>
<li>type（类型）</li>
<li>value（值）    </li>
</ul>
</li>
<li><p>列表就是一个可变对象</p>
<ul>
<li>a = [1,2,3]</li>
</ul>
</li>
<li><p>a[0] = 10 （改对象）</p>
<ul>
<li>这个操作是在通过变量去修改对象的值</li>
<li>这种操作不会改变变量所指向的对象    </li>
<li>当我们去修改对象时，如果有其他变量也指向了该对象，则修改也会在其他的变量中体现</li>
</ul>
</li>
<li><p>a = [4,5,6] （改变量）</p>
<ul>
<li>这个操作是在给变量重新赋值</li>
<li>这种操作会改变变量所指向的对象</li>
<li>为一个变量重新赋值时，不会影响其他的变量</li>
</ul>
</li>
<li><p>一般只有在为变量赋值时才是修改变量，其余的都是修改对象</p>
<h3 id="从数据类型角度理解"><a href="#从数据类型角度理解" class="headerlink" title="从数据类型角度理解"></a>从数据类型角度理解</h3></li>
</ul>
<ol>
<li><p>构造操作:  这类操作主要是基于一些已知信息，产生这个类的实例对象。类似银行进行开通账户  </p>
</li>
<li><p>解析操作：这类操作是获取对象的一些有用信息，其结果反应了对象的一些特征，但返回的不是对象本身。类似银行查询账户余额   </p>
</li>
<li><p>变化操作 ：这类操作是修改对象内部的信息和状态。比如一个银行账户进行转账操作</p>
</li>
</ol>
<div class="note default flat"><p>如果一个类型，具只具有1和2两种操作，也就说只具有构造和解析操作，那么这个类型就是不可变类型，这个类型的对象就是不可变对象<br>如果一个类型，具有1、2、3三种操作，这个类型就是可变类型，这个类型的对象就是可变对象。</p>
</div>

<h2 id="变量和对象"><a href="#变量和对象" class="headerlink" title="变量和对象"></a>变量和对象</h2><ul>
<li>对象并没有直接存储到变量中，在Python中变量更像是给对象起了一个别名</li>
<li>变量中存储的不是对象的值，而是对象的id（内存地址），<ul>
<li>当我们使用变量时，实际上就是在通过对象id在查找对象</li>
</ul>
</li>
<li>变量中保存的对象，只有在为变量重新赋值时才会改变</li>
<li>变量和变量之间是相互独立的，修改一个变量不会影响另一个变量</li>
</ul>
<p><img src="6%E3%80%81%E5%8F%98%E9%87%8F%E4%B8%8E%E5%AF%B9%E8%B1%A1.png" alt="变量与对象"></p>
<h2 id="类型转换"><a href="#类型转换" class="headerlink" title="类型转换"></a>类型转换</h2><ol>
<li><p>所谓的类型转换，将一个类型的对象转换为其他对象</p>
</li>
<li><p><font color=red size=3><strong>类型转换不是改变对象本身的类型，而是根据当前对象的值创建一个新对象</strong></font></p>
</li>
<li><p>类型转换四个函数 int() float() str() bool()</p>
</li>
<li><p><font color=red size=3><strong>type(变量名)可以查看变量的类型</strong></font></p>
</li>
</ol>
<h3 id="int-和float"><a href="#int-和float" class="headerlink" title="int()和float()"></a>int()和float()</h3><ol>
<li>int() 可以用来将其他的对象转换为整型</li>
<li>规则：</li>
</ol>
<ul>
<li>布尔值：True -&gt; 1   False -&gt; 0</li>
<li>浮点数：直接取整，省略小数点后的内容</li>
<li>字符串：合法的整数字符串，直接转换为对应的数字<ul>
<li>如果不是一个合法的整数字符串，则报错 ValueError: invalid literal for int() with base 10: ‘11.5’</li>
</ul>
</li>
<li>对于其他不可转换为整型的对象，直接抛出异常 ValueError</li>
</ul>
<ol start="3">
<li>float() 和 int()基本一致，不同的是它会将对象转换为浮点数</li>
</ol>
<h3 id="str-和bool"><a href="#str-和bool" class="headerlink" title="str()和bool()"></a>str()和bool()</h3><ol>
<li>str() 可以将对象转换为字符串</li>
</ol>
<ul>
<li>True -&gt; ‘True’</li>
<li>False -&gt; ‘False’</li>
<li>123 -&gt; ‘123’ </li>
<li>。。。</li>
</ul>
<ol start="2">
<li>bool() 可以将对象转换为布尔值，任何对象都可以转换为布尔值</li>
</ol>
<ul>
<li>规则：对于所有表示空性的对象都会转换为False，其余的转换为True</li>
<li>哪些表示的空性：0 、 None 、 ‘’ 。。。</li>
</ul>
<h3 id="例子说明"><a href="#例子说明" class="headerlink" title="例子说明"></a>例子说明</h3><p>a = True</p>
<ul>
<li>调用int()来将a转换为整型<br>int(a)  这里a的类型及值都没有发生改变</li>
<li>int()函数不会对原来的变量产生影响，他是对象转换为指定的类型并将其作为返回值返回</li>
<li>如果希望修改原来的变量，则需要对变量进行重新赋值<br>a = int(a)</li>
</ul>
<h1 id="第四章-流程控制语句"><a href="#第四章-流程控制语句" class="headerlink" title="第四章 流程控制语句"></a>第四章 流程控制语句</h1><div class="note primary flat"><ul>
<li>缩进有两种方式，一种是使用tab键，一种是使用空格（四个）<ul>
<li>Python的官方文档中推荐我们使用空格来缩进</li>
<li>在sublime text中可以设置：”translate_tabs_to_spaces”: true</li>
</ul>
</li>
</ul>
</div>
<h2 id="条件分支"><a href="#条件分支" class="headerlink" title="条件分支"></a>条件分支</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件：</span><br><span class="line">    条件为true执行的操作</span><br><span class="line"><span class="keyword">else</span>：</span><br><span class="line">    条件为false执行的操作</span><br><span class="line">  </span><br><span class="line"><span class="comment">#if-else例子</span></span><br><span class="line">age = <span class="number">7</span></span><br><span class="line"><span class="keyword">if</span> age &gt; <span class="number">17</span> :</span><br><span class="line">    print(<span class="string">&#x27;你已经成年了~~&#x27;</span>)</span><br><span class="line"><span class="keyword">else</span> :</span><br><span class="line">    print(<span class="string">&#x27;你还未成年~~&#x27;</span>)</span><br><span class="line">    </span><br><span class="line"><span class="comment">#if-elif-else例子</span></span><br><span class="line">age = <span class="number">68</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> age &gt;= <span class="number">18</span> <span class="keyword">and</span> age &lt; <span class="number">30</span> :</span><br><span class="line">    print(<span class="string">&#x27;你已经成年了！&#x27;</span>)</span><br><span class="line"><span class="keyword">elif</span> age &gt;= <span class="number">30</span> <span class="keyword">and</span> age &lt; <span class="number">60</span> :</span><br><span class="line">    print(<span class="string">&#x27;你已经中年了！&#x27;</span>)</span><br><span class="line"><span class="keyword">else</span> :</span><br><span class="line">    print(<span class="string">&#x27;你已经退休了！&#x27;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="循环"><a href="#循环" class="headerlink" title="循环"></a>循环</h2><h3 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a>while循环</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> 条件：</span><br><span class="line">    条件为true执行的操作</span><br><span class="line"><span class="keyword">else</span> :</span><br><span class="line">    <span class="keyword">while</span>条件为false执行的操作，为可选择关键词</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="comment">#while例子</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>i = <span class="number">0</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">while</span> i &lt; <span class="number">10</span>:</span><br><span class="line"><span class="meta">... </span>    i += <span class="number">1</span></span><br><span class="line"><span class="meta">... </span>    print(i,<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line"><span class="meta">... </span><span class="keyword">else</span> :</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;else中的代码块&#x27;</span>)</span><br><span class="line">...</span><br><span class="line"><span class="number">1</span> hello</span><br><span class="line"><span class="number">2</span> hello</span><br><span class="line"><span class="number">3</span> hello</span><br><span class="line"><span class="number">4</span> hello</span><br><span class="line"><span class="number">5</span> hello</span><br><span class="line"><span class="number">6</span> hello</span><br><span class="line"><span class="number">7</span> hello</span><br><span class="line"><span class="number">8</span> hello</span><br><span class="line"><span class="number">9</span> hello</span><br><span class="line"><span class="number">10</span> hello</span><br><span class="line"><span class="keyword">else</span>中的代码块</span><br></pre></td></tr></table></figure>
<h3 id="for循环"><a href="#for循环" class="headerlink" title="for循环"></a>for循环</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> 目标 <span class="keyword">in</span> 表达式：</span><br><span class="line">    循环体</span><br></pre></td></tr></table></figure>
<h2 id="条件表达式（三元操作符）"><a href="#条件表达式（三元操作符）" class="headerlink" title="条件表达式（三元操作符）"></a>条件表达式（三元操作符）</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">x,y = <span class="number">4</span>,<span class="number">5</span></span><br><span class="line"><span class="keyword">if</span> x &lt; y:</span><br><span class="line">    small = x</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    small = y</span><br><span class="line">    </span><br><span class="line"><span class="comment">#改为如下：</span></span><br><span class="line">small = x <span class="keyword">if</span> x &lt; y <span class="keyword">else</span> y</span><br></pre></td></tr></table></figure>
<h2 id="break和continue"><a href="#break和continue" class="headerlink" title="break和continue"></a>break和continue</h2><ol>
<li>break: break可以用来立即退出循环语句（包括else）</li>
<li>continue: continue可以用来跳过当次循环</li>
<li>break和continue都是只对离他最近的循环起作用</li>
<li>pass: pass是用来在判断或循环语句中占位的,无实际意义</li>
</ol>
<h2 id="assert-：断言"><a href="#assert-：断言" class="headerlink" title="assert ：断言"></a>assert ：断言</h2><p>如果错误会自动抛出AssertionError异常</p>
<h1 id="第五章-序列"><a href="#第五章-序列" class="headerlink" title="第五章 序列"></a>第五章 序列</h1><h2 id="列表-一个打了激素的数组"><a href="#列表-一个打了激素的数组" class="headerlink" title="列表:一个打了激素的数组"></a>列表:一个打了激素的数组</h2><div class="note primary flat"><p>由于python中变量没有数据类型，所以python中用列表替代数组</p>
</div>

<ol>
<li>列表是Python中的一个对象</li>
<li>对象（object）就是内存中专门用来存储数据的一块区域</li>
<li>之前我们学习的对象，像数值，它只能保存一个单一的数据</li>
<li>列表中可以保存多个有序的数据</li>
<li>列表是用来存储对象的对象</li>
<li>列表的使用：</li>
</ol>
<ul>
<li>列表的创建</li>
<li>操作列表中的数据</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment"># 创建列表，通过[]来创建列表</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_list = [] <span class="comment"># 创建了一个空列表</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(my_list , <span class="built_in">type</span>(my_list))</span><br><span class="line">[] &lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">list</span>&#x27;&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"># 当向列表中添加多个元素时，多个元素之间使用,隔开</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>member = [<span class="string">&#x27;红色&#x27;</span>,<span class="string">&#x27;黑夜&#x27;</span>,<span class="string">&#x27;迷途&#x27;</span>,‘蔡贤权<span class="string">&#x27;,&#x27;</span>旭方<span class="string">&#x27;]</span></span><br><span class="line"><span class="string">&gt;&gt;&gt; member</span></span><br><span class="line"><span class="string">[&#x27;</span>红色<span class="string">&#x27;,&#x27;</span>黑夜<span class="string">&#x27;,&#x27;</span>迷途<span class="string">&#x27;,‘蔡贤权&#x27;</span>,<span class="string">&#x27;旭方&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">#列表嵌套列表</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>mix = [<span class="number">1</span>,<span class="string">&#x27;红色&#x27;</span>,<span class="number">3.14</span>,[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>mix</span><br><span class="line">[<span class="number">1</span>,<span class="string">&#x27;红色&#x27;</span>,<span class="number">3.14</span>,[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 列表中可以保存任意的对象</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_list = [<span class="number">10</span>,<span class="string">&#x27;hello&#x27;</span>,<span class="literal">True</span>,<span class="literal">None</span>,[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],<span class="built_in">print</span>]</span><br></pre></td></tr></table></figure>
<ol start="7">
<li>列表中的对象都会按照插入的顺序存储到列表中，</li>
</ol>
<ul>
<li>第一个插入的对象保存到第一个位置，第二个保存到第二个位置</li>
</ul>
<ol start="8">
<li>我们可以通过索引（index）来获取列表中的元素</li>
</ol>
<ul>
<li>索引是元素在列表中的位置，列表中的每一个元素都有一个索引</li>
<li>索引是从0开始的整数，列表第一个位置索引为0，第二个位置索引为1，第三个位置索引为2，以此类推</li>
</ul>
<ol start="9">
<li><p>索引访问list中每一个位置的元素：list名[n],从0开始；用-1做索引，直接获取最后一个元素<br><img src="8%E3%80%81%E7%B4%A2%E5%BC%95%E8%AE%BF%E9%97%AE%E5%88%97%E8%A1%A8.png" alt="索引访问列表"></p>
</li>
<li><p>追加元素到末尾，将元素插入到指定的位置<br><img src="9%E3%80%81%E6%8F%92%E5%85%A5%E5%88%B0%E6%8C%87%E5%AE%9A%E4%BD%8D%E7%BD%AE.png" alt="插入到指定位置"></p>
</li>
<li><p>len（list名）：获得list元素的个数</p>
</li>
<li><p>extend(列表)：多个数据添加，以列表形式</p>
</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">member.extend([<span class="string">&#x27;竹林小溪&#x27;</span>,<span class="string">&#x27;Crazy迷恋&#x27;</span>])</span><br></pre></td></tr></table></figure>
<h3 id="列表的方法"><a href="#列表的方法" class="headerlink" title="列表的方法"></a>列表的方法</h3><ol>
<li>append()：向列表的最后添加一个元素</li>
<li>insert()：向列表的指定位置插入一个元素</li>
</ol>
<ul>
<li>参数：1.要插入的位置；2.要插入的元素</li>
</ul>
<ol start="3">
<li>extend()：使用新的序列来扩展当前序列，需要一个序列作为参数，它会将该序列中的元素添加到当前列表中</li>
<li>clear()：清空序列</li>
<li>pop()：根据索引删除并返回被删除的元素</li>
<li>remove()：删除指定值得元素，如果相同值得元素有多个，只会删除第一个</li>
<li>reverse(): 用来反转列表</li>
<li>sort()：用来对列表中的元素进行排序，默认是升序排列</li>
</ol>
<ul>
<li>如果需要降序排列，则需要传递一个reverse=True作为参数<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus = [<span class="string">&#x27;孙悟空&#x27;</span>,<span class="string">&#x27;猪八戒&#x27;</span>,<span class="string">&#x27;沙和尚&#x27;</span>,<span class="string">&#x27;唐僧&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus.append(<span class="string">&#x27;唐僧&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;孙悟空&#x27;</span>, <span class="string">&#x27;猪八戒&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus.insert(<span class="number">2</span>,<span class="string">&#x27;唐僧&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;孙悟空&#x27;</span>, <span class="string">&#x27;猪八戒&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus.extend([<span class="string">&#x27;唐僧&#x27;</span>,<span class="string">&#x27;白骨精&#x27;</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;孙悟空&#x27;</span>, <span class="string">&#x27;猪八戒&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus += [<span class="string">&#x27;唐僧&#x27;</span>,<span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;孙悟空&#x27;</span>, <span class="string">&#x27;猪八戒&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;白骨精&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus.clear()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus = [<span class="string">&#x27;孙悟空&#x27;</span>,<span class="string">&#x27;猪八戒&#x27;</span>,<span class="string">&#x27;沙和尚&#x27;</span>,<span class="string">&#x27;唐僧&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>result = stus.pop(<span class="number">2</span>) <span class="comment"># 删除索引为2的元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;result = &#x27;</span>,result)</span><br><span class="line">result =  沙和尚</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>result = stus.pop() <span class="comment"># 删除最后一个</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;result = &#x27;</span>,result)</span><br><span class="line">result =  唐僧</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;孙悟空&#x27;</span>, <span class="string">&#x27;猪八戒&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus.remove(<span class="string">&#x27;猪八戒&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;孙悟空&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_list = [<span class="number">10</span>,<span class="number">1</span>,<span class="number">20</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">0</span>,-<span class="number">2</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_list.sort(reverse=<span class="literal">True</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;修改后&#x27;</span>,my_list)</span><br><span class="line">修改后 [<span class="number">20</span>, <span class="number">10</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">0</span>, -<span class="number">2</span>]</span><br></pre></td></tr></table></figure>
<h3 id="遍历列表"><a href="#遍历列表" class="headerlink" title="遍历列表"></a>遍历列表</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">stus = [<span class="string">&#x27;孙悟空&#x27;</span>,<span class="string">&#x27;猪八戒&#x27;</span>,<span class="string">&#x27;沙和尚&#x27;</span>,<span class="string">&#x27;唐僧&#x27;</span>,<span class="string">&#x27;白骨精&#x27;</span>,<span class="string">&#x27;蜘蛛精&#x27;</span>]</span><br><span class="line"><span class="comment"># 通过while循环来遍历列表</span></span><br><span class="line">i = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> i &lt; <span class="built_in">len</span>(stus):</span><br><span class="line">    print(stus[i])</span><br><span class="line">    i += <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过for循环来遍历列表</span></span><br><span class="line"><span class="comment"># 语法：</span></span><br><span class="line"><span class="comment">#   for 变量 in 序列 :</span></span><br><span class="line"><span class="comment">#       代码块</span></span><br><span class="line"><span class="comment"># for循环的代码块会执行多次，序列中有几个元素就会执行几次</span></span><br><span class="line"><span class="comment">#   没执行一次就会将序列中的一个元素赋值给变量，</span></span><br><span class="line"><span class="comment">#   所以我们可以通过变量，来获取列表中的元素</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> s <span class="keyword">in</span> stus :</span><br><span class="line">    print(s)</span><br></pre></td></tr></table></figure>
<h3 id="列表切片（slice）"><a href="#列表切片（slice）" class="headerlink" title="列表切片（slice）"></a>列表切片（slice）</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>member</span><br><span class="line">[<span class="string">&#x27;牡丹&#x27;</span>,<span class="string">&#x27;黑夜&#x27;</span>,<span class="string">&#x27;迷途&#x27;</span>,<span class="string">&#x27;福禄娃娃&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>member[<span class="number">1</span>,<span class="number">3</span>]      <span class="comment">#从1切到2，包含两个</span></span><br><span class="line">[<span class="string">&#x27;黑夜&#x27;</span>,<span class="string">&#x27;迷途&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>member[:<span class="number">3</span>] <span class="comment">#从1切到2，包含三个</span></span><br><span class="line">[<span class="string">&#x27;牡丹&#x27;</span>,<span class="string">&#x27;黑夜&#x27;</span>,<span class="string">&#x27;迷途&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>member[:]  <span class="comment">#相当于拷贝</span></span><br><span class="line">[<span class="string">&#x27;牡丹&#x27;</span>,<span class="string">&#x27;黑夜&#x27;</span>,<span class="string">&#x27;迷途&#x27;</span>,<span class="string">&#x27;福禄娃娃&#x27;</span>]</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  如果直接赋值，如下</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>member1 = member</span><br><span class="line">  这时，对member的任何修改，member1同样生效，相当于Linux中的硬连接</span><br></pre></td></tr></table></figure>
<h3 id="删除元素和修改元素"><a href="#删除元素和修改元素" class="headerlink" title="删除元素和修改元素"></a>删除元素和修改元素</h3></li>
</ul>
<ol>
<li>列表名.remove(‘存储的值’)：删除指定值</li>
<li>del 列表名[索引]：删除指定索引位置所在的值</li>
<li>del 列表名：删除这个列表</li>
<li>列表名.pop()：删除最后一个元素</li>
<li>列表名.pop(2): 删除指定位置的元素</li>
<li>pop(i)方法：删除指定位置的元素，i为索引位置<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>member.remove(<span class="string">&#x27;蔡贤权&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> member[<span class="number">1</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> member \\删除这个列表</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>member.pop()   \\删除最后一个元素</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>member.pop(<span class="number">2</span>)</span><br></pre></td></tr></table></figure></li>
<li>通过切片来修改列表和删除元素</li>
</ol>
<ul>
<li>通过切片来修改列表，在给切片进行赋值时，只能使用序列<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus = [<span class="string">&#x27;孙悟空&#x27;</span>,<span class="string">&#x27;猪八戒&#x27;</span>,<span class="string">&#x27;沙和尚&#x27;</span>,<span class="string">&#x27;唐僧&#x27;</span>,<span class="string">&#x27;蜘蛛精&#x27;</span>,<span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"><span class="comment"># 在给切片进行赋值时，只能使用序列</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus[<span class="number">0</span>:<span class="number">2</span>] = [<span class="string">&#x27;牛魔王&#x27;</span>,<span class="string">&#x27;红孩儿&#x27;</span>] <span class="comment">#使用新的元素替换旧元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;牛魔王&#x27;</span>, <span class="string">&#x27;红孩儿&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;蜘蛛精&#x27;</span>, <span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 也可以多个元素来替换之前的元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus[<span class="number">0</span>:<span class="number">2</span>] = [<span class="string">&#x27;牛魔王&#x27;</span>,<span class="string">&#x27;红孩儿&#x27;</span>,<span class="string">&#x27;二郎神&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;牛魔王&#x27;</span>, <span class="string">&#x27;红孩儿&#x27;</span>, <span class="string">&#x27;二郎神&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;蜘蛛精&#x27;</span>, <span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus[<span class="number">0</span>:<span class="number">0</span>] = [<span class="string">&#x27;铁扇公主&#x27;</span>] <span class="comment">#向索引为0的位置插入元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;铁扇公主&#x27;</span>, <span class="string">&#x27;牛魔王&#x27;</span>, <span class="string">&#x27;红孩儿&#x27;</span>, <span class="string">&#x27;二郎神&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;蜘蛛精&#x27;</span>, <span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 当设置了步长时，序列中元素的个数必须和切片中元素的个数一致</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus[::<span class="number">2</span>]</span><br><span class="line">[<span class="string">&#x27;铁扇公主&#x27;</span>, <span class="string">&#x27;红孩儿&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;蜘蛛精&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus[::<span class="number">2</span>] = [<span class="string">&#x27;牛魔王&#x27;</span>,<span class="string">&#x27;红孩儿&#x27;</span>,<span class="string">&#x27;二郎神&#x27;</span>] <span class="comment">#这里会报错，由于切片选取出来是四个，而序列中只有三个</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">ValueError: attempt to assign sequence of size <span class="number">3</span> to extended <span class="built_in">slice</span> of size <span class="number">4</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li>通过切片来删除元素<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;铁扇公主&#x27;</span>, <span class="string">&#x27;牛魔王&#x27;</span>, <span class="string">&#x27;红孩儿&#x27;</span>, <span class="string">&#x27;二郎神&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;蜘蛛精&#x27;</span>, <span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> stus[<span class="number">0</span>:<span class="number">2</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;红孩儿&#x27;</span>, <span class="string">&#x27;二郎神&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;蜘蛛精&#x27;</span>, <span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> stus[::<span class="number">2</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;二郎神&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus[<span class="number">1</span>:<span class="number">3</span>] = []</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus)</span><br><span class="line">[<span class="string">&#x27;二郎神&#x27;</span>]</span><br></pre></td></tr></table></figure>
<div class="note primary flat"><p><font color=red size=3><strong>以上操作，只适用于可变序列</strong></font></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = <span class="string">&#x27;hello&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s[<span class="number">1</span>] = <span class="string">&#x27;a&#x27;</span> <span class="comment">#不可变序列，无法通过索引来修改</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">TypeError: <span class="string">&#x27;str&#x27;</span> <span class="built_in">object</span> does <span class="keyword">not</span> support item assignment</span><br><span class="line"></span><br><span class="line"><span class="comment"># 可以通过 list() 函数将其他的序列转换为list</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = <span class="built_in">list</span>(s)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(s)</span><br><span class="line">[<span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;o&#x27;</span>]</span><br></pre></td></tr></table></figure></div>


</li>
</ul>
<h2 id="通用操作-序列"><a href="#通用操作-序列" class="headerlink" title="通用操作(序列)"></a>通用操作(序列)</h2><h3 id="和"><a href="#和" class="headerlink" title="+ 和 *"></a>+ 和 *</h3><ul>
<li>“+”可以将两个列表拼接为一个列表</li>
<li>“*”可以将列表重复指定的次数<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_list = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>] * <span class="number">5</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_list</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br></pre></td></tr></table></figure>
<h3 id="in-和-not-in"><a href="#in-和-not-in" class="headerlink" title="in 和 not in"></a>in 和 not in</h3></li>
<li>in用来检查指定元素是否存在于列表中，如果存在，返回True，否则返回False</li>
<li>not in用来检查指定元素是否不在列表中，如果不在，返回True，否则返回False<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus = [<span class="string">&#x27;孙悟空&#x27;</span>,<span class="string">&#x27;猪八戒&#x27;</span>,<span class="string">&#x27;沙和尚&#x27;</span>,<span class="string">&#x27;唐僧&#x27;</span>,<span class="string">&#x27;蜘蛛精&#x27;</span>,<span class="string">&#x27;白骨精&#x27;</span>,<span class="string">&#x27;沙和尚&#x27;</span>,<span class="string">&#x27;沙和尚&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;牛魔王&#x27;</span> <span class="keyword">not</span> <span class="keyword">in</span> stus)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;牛魔王&#x27;</span> <span class="keyword">in</span> stus)</span><br><span class="line"><span class="literal">False</span></span><br></pre></td></tr></table></figure>
<h3 id="、-、is、is-not"><a href="#、-、is、is-not" class="headerlink" title="==、!=、is、is not"></a>==、!=、is、is not</h3></li>
</ul>
<ol>
<li>==、!= 比较的是对象的值是否相等 </li>
<li>is、is not 比较的是对象的id是否相等（比较两个对象是否是同一个对象）<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(a,b)</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>] [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="built_in">id</span>(a),<span class="built_in">id</span>(b))</span><br><span class="line"><span class="number">1962841246920</span> <span class="number">1962841246792</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(a == b) <span class="comment"># a和b的值相等，使用==会返回True</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(a <span class="keyword">is</span> b) <span class="comment"># a和b不是同一个对象，内存地址不同，使用is会返回False</span></span><br><span class="line"><span class="literal">False</span></span><br></pre></td></tr></table></figure>
<h3 id="len-、min-和max"><a href="#len-、min-和max" class="headerlink" title="len()、min()和max()"></a>len()、min()和max()</h3></li>
</ol>
<ul>
<li>len() 获取列表中的元素的个数</li>
<li>min() 获取列表中的最小值</li>
<li>max() 获取列表中的最大值<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>arr = [<span class="number">10</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">5</span>,<span class="number">100</span>,<span class="number">77</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="built_in">min</span>(arr),<span class="built_in">max</span>(arr),<span class="built_in">len</span>(arr))</span><br><span class="line"><span class="number">1</span> <span class="number">100</span> <span class="number">6</span></span><br></pre></td></tr></table></figure>
<h3 id="一些常见的方法"><a href="#一些常见的方法" class="headerlink" title="一些常见的方法"></a>一些常见的方法</h3></li>
</ul>
<ol>
<li>index()：取索引</li>
<li>count()：统计次数</li>
<li>reversed()：生成一个迭代器，强制转换为list显示</li>
<li>enumerate()：增加序号</li>
<li>zip()：拉链表<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>stus = [<span class="string">&#x27;孙悟空&#x27;</span>,<span class="string">&#x27;猪八戒&#x27;</span>,<span class="string">&#x27;沙和尚&#x27;</span>,<span class="string">&#x27;唐僧&#x27;</span>,<span class="string">&#x27;蜘蛛精&#x27;</span>,<span class="string">&#x27;白骨精&#x27;</span>,<span class="string">&#x27;沙和尚&#x27;</span>,<span class="string">&#x27;沙和尚&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">#获取指定元素在列表中的第一次出现时索引</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus.index(<span class="string">&#x27;沙和尚&#x27;</span>))</span><br><span class="line"><span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#index()的第二个参数，表示查找的起始位置，第三个参数，表示查找的结束位置</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus.index(<span class="string">&#x27;沙和尚&#x27;</span>,<span class="number">3</span>,<span class="number">7</span>))</span><br><span class="line"><span class="number">6</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># count()统计指定元素在列表中出现的次数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(stus.count(<span class="string">&#x27;牛魔王&#x27;</span>))</span><br><span class="line"><span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#reversed()：生成一个迭代器，强制转换为list显示</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>numbers = [<span class="number">1</span>,<span class="number">18</span>,<span class="number">13</span>,<span class="number">0</span>,-<span class="number">98</span>,<span class="number">34</span>,<span class="number">54</span>,<span class="number">76</span>,<span class="number">32</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="built_in">reversed</span>(numbers))</span><br><span class="line">[<span class="number">32</span>, <span class="number">76</span>, <span class="number">54</span>, <span class="number">34</span>, -<span class="number">98</span>, <span class="number">0</span>, <span class="number">13</span>, <span class="number">18</span>, <span class="number">1</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">#enumerate()：增加序号</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="built_in">enumerate</span>(numbers))</span><br><span class="line">[(<span class="number">0</span>, <span class="number">1</span>), (<span class="number">1</span>, <span class="number">18</span>), (<span class="number">2</span>, <span class="number">13</span>), (<span class="number">3</span>, <span class="number">0</span>), (<span class="number">4</span>, -<span class="number">98</span>), (<span class="number">5</span>, <span class="number">34</span>), (<span class="number">6</span>, <span class="number">54</span>), (<span class="number">7</span>, <span class="number">76</span>), (<span class="number">8</span>, <span class="number">32</span>)]</span><br><span class="line"></span><br><span class="line"><span class="comment">#zip()：拉链表</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">list</span>(<span class="built_in">zip</span>(a,b))</span><br><span class="line">[(<span class="number">1</span>, <span class="number">4</span>), (<span class="number">2</span>, <span class="number">5</span>), (<span class="number">3</span>, <span class="number">6</span>), (<span class="number">4</span>, <span class="number">7</span>), (<span class="number">5</span>, <span class="number">8</span>)]</span><br></pre></td></tr></table></figure>
<h3 id="range"><a href="#range" class="headerlink" title="range()"></a>range()</h3>range()是一个函数，可以用来生成一个自然数的序列</li>
</ol>
<ul>
<li>该函数需要三个参数<ul>
<li>起始位置（可以省略，默认是0）</li>
<li>结束位置【不包含边界位置】</li>
<li>步长（可以省略，默认是1）<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">r &#x3D; range(5) # 生成一个这样的序列[0,1,2,3,4]</span><br><span class="line">r &#x3D; range(0,10,2)</span><br><span class="line">r &#x3D; range(10,0,-1) #从10开始，往0开始生成，[10,9,8,7,6,5,4,3,2,1]</span><br><span class="line"></span><br><span class="line">&gt;&gt;&gt; for s in &#39;hello&#39;:</span><br><span class="line">...     print(s)</span><br><span class="line">...</span><br><span class="line">h</span><br><span class="line">e</span><br><span class="line">l</span><br><span class="line">l</span><br><span class="line">o</span><br></pre></td></tr></table></figure>
<h2 id="序列（sequence）"><a href="#序列（sequence）" class="headerlink" title="序列（sequence）"></a>序列（sequence）</h2><div class="note primary flat"><p>即对列表、元组、字符串的内置函数的操作</p>
</div>
</li>
</ul>
</li>
</ul>
<ol>
<li>序列是Python中最基本的一种数据结构</li>
<li>数据结构指计算机中数据存储的方式</li>
<li>序列用于保存一组有序的数据，所有的数据在序列当中都有一个唯一的位置（索引）</li>
</ol>
<ul>
<li>并且序列中的数据会按照添加的顺序来分配索引</li>
</ul>
<ol start="4">
<li>序列的分类：</li>
</ol>
<ul>
<li>可变序列（<font color=red size=3><strong>序列中的元素可以改变</strong></font>）：<ul>
<li>&gt; 列表（list）</li>
</ul>
</li>
<li>不可变序列（<font color=red size=3><strong>序列中的元素不能改变</strong></font>）：<ul>
<li>&gt; 字符串（str）    </li>
<li>&gt; 元组（tuple）</li>
</ul>
</li>
<li>刚刚我们所讲所有操作都是序列的通用操作01 02 03 三个文件中的操作</li>
</ul>
<ol start="5">
<li>列表、元组和字符串的共同点：</li>
</ol>
<ul>
<li>都可以通过索引得到每一个元素</li>
<li>默认索引值总是从0开始</li>
<li>可以通过分片的方法得到一个范围内的元素的集合</li>
<li>有很多共同的操作符（重复操作符、拼接操作符、成员关系操作符）</li>
</ul>
<div class="note primary flat"><p>迭代：迭代是重复反馈过程的活动，其目的通常是为了逼近所需目标或结果。每一次对过程的重复称为一次“迭代”，而每一次迭代得到的结果会作为下一次迭代的初始值。<br> —百度百科</p>
</div>

<h2 id="元组：戴上了枷锁的列表"><a href="#元组：戴上了枷锁的列表" class="headerlink" title="元组：戴上了枷锁的列表"></a>元组：戴上了枷锁的列表</h2><ol>
<li>元组是一个不可变的序列</li>
<li>它的操作的方式基本上和列表是一致的</li>
<li>所以你在操作元组时，就把元组当成是一个不可变的列表就ok了</li>
<li>一般当我们希望数据不改变时，就使用元组，其余情况都使用列表<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建元组</span></span><br><span class="line"><span class="comment"># 使用()来创建元组</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_tuple = () <span class="comment"># 创建了一个空元组</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(my_tuple,<span class="built_in">type</span>(my_tuple))</span><br><span class="line">() &lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">tuple</span>&#x27;&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line">&gt;&gt;&gt; my_tuple = (1,2,3,4,5) # 创建了一个5个元素的元组</span><br><span class="line"></span><br><span class="line"><span class="comment"># 元组是不可变对象，不能尝试为元组中的元素重新赋值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_tuple[<span class="number">3</span>] = <span class="number">10</span></span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">TypeError: <span class="string">&#x27;tuple&#x27;</span> <span class="built_in">object</span> does <span class="keyword">not</span> support item assignment</span><br><span class="line"></span><br><span class="line"><span class="comment">#获取元组中的元素</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(my_tuple[<span class="number">3</span>])</span><br><span class="line"><span class="number">4</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 当元组不是空元组时，括号可以省略</span></span><br><span class="line"><span class="comment"># 如果元组不是空元组，它里边至少要有一个逗号“,”，否则按数学公式中的小括号表示，即只是一个数值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_tuple = <span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_tuple = <span class="number">40</span>,</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(my_tuple,<span class="built_in">type</span>(my_tuple))</span><br><span class="line">(<span class="number">40</span>,) &lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">tuple</span>&#x27;&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"># 元组的解包（解构）</span></span><br><span class="line"><span class="class"># 解包指就是将元组当中每一个元素都赋值给一个变量</span></span><br><span class="line">&gt;&gt;&gt; my_tuple = 10,20,30,40</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a,b,c,d = my_tuple</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&quot;a =&quot;</span>,a)</span><br><span class="line">a = <span class="number">10</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&quot;b =&quot;</span>,b)</span><br><span class="line">b = <span class="number">20</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&quot;c =&quot;</span>,c)</span><br><span class="line">c = <span class="number">30</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&quot;d =&quot;</span>,d)</span><br><span class="line">d = <span class="number">40</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="number">100</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = <span class="number">300</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(a , b)</span><br><span class="line"><span class="number">100</span> <span class="number">300</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 交互a 和 b的值，这时我们就可以利用元组的解包</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a , b = b , a</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(a , b)</span><br><span class="line"><span class="number">300</span> <span class="number">100</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 在对一个元组进行解包时，变量的数量必须和元组中的元素的数量一致</span></span><br><span class="line"><span class="comment"># 也可以在变量前边添加一个*，这样变量将会获取元组中所有剩余的元素</span></span><br><span class="line"><span class="comment"># 不能同时出现两个或以上的*变量</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>my_tuple = <span class="number">10</span> , <span class="number">20</span> , <span class="number">30</span> , <span class="number">40</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a , b , *c = my_tuple</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="built_in">type</span>(c))</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">list</span>&#x27;&gt;</span></span><br><span class="line"><span class="class">&gt;&gt;&gt; <span class="title">print</span>(<span class="params">c</span>)</span></span><br><span class="line">[30, 40]</span><br><span class="line"></span><br><span class="line"><span class="comment">#*号放在中间变量也是可以取值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a , *b , c = my_tuple</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(b)</span><br><span class="line">[<span class="number">20</span>, <span class="number">30</span>]</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>*a , *b , c = my_tuple</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span></span><br><span class="line">SyntaxError: two starred expressions <span class="keyword">in</span> assignment</span><br><span class="line"></span><br><span class="line"><span class="comment">#也可以作用于字符串</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a , b , *c = <span class="string">&#x27;hello world&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="built_in">type</span>(c))</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">list</span>&#x27;&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h2></li>
</ol>
<h3 id="格式化字符串"><a href="#格式化字符串" class="headerlink" title="格式化字符串"></a>格式化字符串</h3><ol>
<li>如果将两个字符串进行相加，则会自动将两个字符串拼接为一个<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="string">&#x27;abc&#x27;</span> + <span class="string">&#x27;haha&#x27;</span> + <span class="string">&#x27;哈哈&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line"><span class="string">&#x27;abchaha哈哈&#x27;</span></span><br></pre></td></tr></table></figure></li>
<li>在创建字符串时，可以在字符串中指定占位符</li>
</ol>
<ul>
<li><p>%s 在字符串中表示任意字符</p>
</li>
<li><p>%f 浮点数占位符</p>
</li>
<li><p>%d 整数占位符</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = <span class="string">&#x27;Hello %s&#x27;</span>%<span class="string">&#x27;孙悟空&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b</span><br><span class="line"><span class="string">&#x27;Hello 孙悟空&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = <span class="string">&#x27;hello %s 你好 %s&#x27;</span>%(<span class="string">&#x27;tom&#x27;</span>,<span class="string">&#x27;孙悟空&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b</span><br><span class="line"><span class="string">&#x27;hello tom 你好 孙悟空&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = <span class="string">&#x27;hello %3.5s&#x27;</span>%<span class="string">&#x27;abcdefg&#x27;</span> <span class="comment"># %3.5s字符串的长度限制在3-5之间</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b</span><br><span class="line"><span class="string">&#x27;hello abcde&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = <span class="string">&#x27;hello %s&#x27;</span>%<span class="number">123.456</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b</span><br><span class="line"><span class="string">&#x27;hello 123.456&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = <span class="string">&#x27;hello %.2f&#x27;</span>%<span class="number">123.456</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b</span><br><span class="line"><span class="string">&#x27;hello 123.46&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = <span class="string">&#x27;hello %d&#x27;</span>%<span class="number">123.95</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b</span><br><span class="line"><span class="string">&#x27;hello 123&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="number">123</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;a = %s&#x27;</span>%a)</span><br><span class="line">a = <span class="number">123</span></span><br></pre></td></tr></table></figure>
<h3 id="复制字符串"><a href="#复制字符串" class="headerlink" title="复制字符串"></a>复制字符串</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 字符串的复制（将字符串和数字相乘）</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="string">&#x27;abc&#x27;</span></span><br><span class="line"><span class="comment"># * 在语言中表示乘法</span></span><br><span class="line"><span class="comment"># 如果将字符串和数字相乘，则解释器会将字符串重复指定的次数并返回</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = a * <span class="number">20</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line"><span class="string">&#x27;abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc&#x27;</span></span><br></pre></td></tr></table></figure>
<h2 id="字典当索引不好用时（映射类型）"><a href="#字典当索引不好用时（映射类型）" class="headerlink" title="字典当索引不好用时（映射类型）"></a>字典当索引不好用时（映射类型）</h2></li>
</ul>
<ol>
<li>字典属于一种新的数据结构，称为映射（mapping）</li>
<li>字典的作用和列表类似，都是用来存储对象的容器</li>
<li><font color=red size=3><strong>列表</strong></font>存储数据的性能很好，但是查询数据的性能的很差</li>
<li>在字典中每一个元素都有一个唯一的名字，通过这个唯一的名字可以快速的查找到指定的元素</li>
<li>在查询元素时，字典的效率是非常快的</li>
<li>在字典中可以保存多个对象，每个对象都会有一个唯一的名字</li>
</ol>
<ul>
<li><p>这个唯一的名字，我们称其为键（key），通过key可以快速的查询value</p>
</li>
<li><p>这个对象，我们称其为值（value）</p>
</li>
<li><p>所以字典，我们也称为叫做键值对（key-value）结构</p>
</li>
<li><p>每个字典中都可以有多个键值对，而每一个键值对我们称其为一项（item）</p>
<h3 id="创建字典"><a href="#创建字典" class="headerlink" title="创建字典"></a>创建字典</h3></li>
<li><p>创建一个保护有数据的字典</p>
</li>
<li><p>语法：</p>
<ul>
<li>{key:value,key:value,key:value}</li>
<li>字典的值可以是任意对象</li>
<li>字典的键可以是任意的不可变对象（int、str、bool、tuple …），但是一般我们都会使用str</li>
<li>字典的键是不能重复的，如果出现重复的后边的会替换到前边的</li>
</ul>
</li>
<li><p>使用dict函数创建字典【<font color=red size=3><strong>关键字参数创建字典时key值不能有引号，key都是字符串</strong></font>】</p>
</li>
<li><p>使用fromkeys()内建字典</p>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用 &#123;&#125; 来创建字典</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d = &#123;&#125; <span class="comment"># 创建了一个空字典</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#使用dict函数创建字典</span></span><br><span class="line"><span class="comment">#这个含义是将一个包含有双值子元组或子序列的元组或序列转换为字典</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict3 = <span class="built_in">dict</span>(((<span class="string">&#x27;F&#x27;</span>,<span class="number">70</span>),(<span class="string">&#x27;i&#x27;</span>,<span class="number">105</span>),(<span class="string">&#x27;s&#x27;</span>,<span class="number">115</span>),(<span class="string">&#x27;h&#x27;</span>,<span class="number">104</span>),(<span class="string">&#x27;c&#x27;</span>,<span class="number">67</span>)))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict3</span><br><span class="line">&#123;<span class="string">&#x27;F&#x27;</span>: <span class="number">70</span>, <span class="string">&#x27;i&#x27;</span>: <span class="number">105</span>, <span class="string">&#x27;s&#x27;</span>: <span class="number">115</span>, <span class="string">&#x27;h&#x27;</span>: <span class="number">104</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">67</span>&#125;</span><br><span class="line"><span class="comment">#注意：这里也可以传入字符串，但必须仅有两个字符</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict4=<span class="built_in">dict</span>([(<span class="number">1</span>,<span class="number">2</span>),[<span class="string">&#x27;d&#x27;</span>,<span class="number">3</span>],<span class="string">&#x27;ab&#x27;</span>,(<span class="number">4</span>,<span class="number">5</span>)])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict4</span><br><span class="line">&#123;<span class="number">1</span>: <span class="number">2</span>, <span class="string">&#x27;d&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;a&#x27;</span>: <span class="string">&#x27;b&#x27;</span>, <span class="number">4</span>: <span class="number">5</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#参数传入的是key=value，一个括号就可以，key不能有引号，都是字符串类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict4 = <span class="built_in">dict</span>(蔡贤权 = <span class="string">&#x27;让编程改变世界&#x27;</span>, 苍井空 = <span class="string">&#x27;让AV征服所有宅男&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict4</span><br><span class="line">&#123;<span class="string">&#x27;蔡贤权&#x27;</span>: <span class="string">&#x27;让编程改变世界&#x27;</span>, <span class="string">&#x27;苍井空&#x27;</span>: <span class="string">&#x27;让AV征服所有宅男&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#使用fromkeys()创建字典</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict1 = &#123;&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict1</span><br><span class="line">&#123;&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict1.fromkeys((<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>))</span><br><span class="line">&#123;<span class="number">1</span>: <span class="literal">None</span>, <span class="number">2</span>: <span class="literal">None</span>, <span class="number">3</span>: <span class="literal">None</span>, <span class="number">4</span>: <span class="literal">None</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict1.fromkeys((<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>),<span class="string">&#x27;Number&#x27;</span>)    <span class="comment">#注意这里的第二个参数只能为一个，因为是将第二个参数的全部值赋予给每一个key</span></span><br><span class="line">&#123;<span class="number">1</span>: <span class="string">&#x27;Number&#x27;</span>, <span class="number">2</span>: <span class="string">&#x27;Number&#x27;</span>, <span class="number">3</span>: <span class="string">&#x27;Number&#x27;</span>, <span class="number">4</span>: <span class="string">&#x27;Number&#x27;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict1.fromkeys((<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>),(<span class="string">&#x27;Number&#x27;</span>,<span class="string">&#x27;caixianquan&#x27;</span>))</span><br><span class="line">&#123;<span class="number">1</span>: (<span class="string">&#x27;Number&#x27;</span>, <span class="string">&#x27;caixianquan&#x27;</span>), <span class="number">2</span>: (<span class="string">&#x27;Number&#x27;</span>, <span class="string">&#x27;caixianquan&#x27;</span>), <span class="number">3</span>: (<span class="string">&#x27;Number&#x27;</span>, <span class="string">&#x27;caixianquan&#x27;</span>), <span class="number">4</span>: (<span class="string">&#x27;Number&#x27;</span>, <span class="string">&#x27;caixianquan&#x27;</span>)&#125;</span><br><span class="line"><span class="comment"># 无法修改，试图修改时会重新创建一个字典</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dict1.fromkeys((<span class="number">1</span>,<span class="number">3</span>),<span class="string">&#x27;xufang&#x27;</span>)</span><br><span class="line">&#123;<span class="number">1</span>: <span class="string">&#x27;xufang&#x27;</span>, <span class="number">3</span>: <span class="string">&#x27;xufang&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 重复值，后面替换前面</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d = &#123;</span><br><span class="line"><span class="meta">... </span><span class="string">&#x27;name&#x27;</span>:<span class="string">&#x27;孙悟空&#x27;</span> ,</span><br><span class="line"><span class="meta">... </span><span class="string">&#x27;age&#x27;</span>:<span class="number">18</span> ,</span><br><span class="line"><span class="meta">... </span><span class="string">&#x27;gender&#x27;</span>:<span class="string">&#x27;男&#x27;</span> ,</span><br><span class="line"><span class="meta">... </span><span class="string">&#x27;name&#x27;</span>:<span class="string">&#x27;sunwukong&#x27;</span></span><br><span class="line"><span class="meta">... </span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(d,<span class="built_in">type</span>(d))</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;sunwukong&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>&#125; &lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">dict</span>&#x27;&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"># 需要根据键来获取值</span></span><br><span class="line"><span class="class">&gt;&gt;&gt; <span class="title">print</span>(<span class="params">d[<span class="string">&#x27;name&#x27;</span>],d[<span class="string">&#x27;age&#x27;</span>],d[<span class="string">&#x27;gender&#x27;</span>]</span>)</span></span><br><span class="line"><span class="class"><span class="title">sunwukong</span> 18 男</span></span><br><span class="line"><span class="class"># 如果使用了字典中不存在的键，会报错</span></span><br><span class="line"><span class="class">&gt;&gt;&gt; <span class="title">print</span>(<span class="params">d[<span class="string">&#x27;hello&#x27;</span>]</span>) <span class="title">KeyError</span>:</span> <span class="string">&#x27;hello&#x27;</span></span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span></span><br><span class="line">    print(d[<span class="string">&#x27;hello&#x27;</span>]) KeyError: <span class="string">&#x27;hello&#x27;</span></span><br><span class="line">                             ^</span><br><span class="line">SyntaxError: invalid syntax</span><br></pre></td></tr></table></figure>
<h3 id="使用字典"><a href="#使用字典" class="headerlink" title="使用字典"></a>使用字典</h3><ol>
<li>keys(): 该方法会返回字典的所有的key。返回一个序列，序列中保存有字典的所有的键</li>
<li>values(): 该方法会返回一个序列，序列中保存有字典的左右的值</li>
<li>items(): 该方法会返回字典中所有的项,返回一个序列，序列中包含有双值子序列。双值分别是，字典中的key和value<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>d = &#123;<span class="string">&#x27;name&#x27;</span>:<span class="string">&#x27;孙悟空&#x27;</span>,<span class="string">&#x27;age&#x27;</span>:<span class="number">18</span>,<span class="string">&#x27;gender&#x27;</span>:<span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#通过遍历keys()来获取所有的键</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> k <span class="keyword">in</span> d.keys() :</span><br><span class="line"><span class="meta">... </span>    print(k , d[k])</span><br><span class="line">...</span><br><span class="line">name 孙悟空</span><br><span class="line">age <span class="number">18</span></span><br><span class="line">gender 男</span><br><span class="line"></span><br><span class="line"><span class="comment"># 只获取字典中的值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> v <span class="keyword">in</span> d.values():</span><br><span class="line"><span class="meta">... </span>    print(v)</span><br><span class="line">...</span><br><span class="line">孙悟空</span><br><span class="line"><span class="number">18</span></span><br><span class="line">男</span><br><span class="line"></span><br><span class="line"><span class="comment">#通过item获取key和value</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(d.items())</span><br><span class="line">dict_items([(<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;孙悟空&#x27;</span>), (<span class="string">&#x27;age&#x27;</span>, <span class="number">18</span>), (<span class="string">&#x27;gender&#x27;</span>, <span class="string">&#x27;男&#x27;</span>)])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">for</span> k,v <span class="keyword">in</span> d.items() :</span><br><span class="line"><span class="meta">... </span>    print(k , <span class="string">&#x27;=&#x27;</span> , v)</span><br><span class="line">...</span><br><span class="line">name = 孙悟空</span><br><span class="line">age = <span class="number">18</span></span><br><span class="line">gender = 男</span><br></pre></td></tr></table></figure></li>
<li>len(): 获取字典中键值对的个数</li>
<li>in、not in: 检查字典中是否包含【不包含】指定的键<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>d = &#123;<span class="string">&#x27;name&#x27;</span>:<span class="string">&#x27;孙悟空&#x27;</span>,<span class="string">&#x27;age&#x27;</span>:<span class="number">18</span>,<span class="string">&#x27;gender&#x27;</span>:<span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line"><span class="comment">#获取字典中键值对的个数</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="built_in">len</span>(d))</span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;hello&#x27;</span> <span class="keyword">in</span> d) <span class="comment"># in 检查字典中是否包含指定的键</span></span><br><span class="line"><span class="literal">False</span></span><br></pre></td></tr></table></figure></li>
<li>get(key[, default]): 该方法用来根据键来获取字典中的值,可以设定取不到时默认返回值</li>
<li>修改字典：如果key存在则覆盖，不存在则添加<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#设置默认值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;孙悟空&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(d.get(<span class="string">&#x27;name&#x27;</span>))</span><br><span class="line">孙悟空</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(d.get(<span class="string">&#x27;hello&#x27;</span>))</span><br><span class="line"><span class="literal">None</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(d.get(<span class="string">&#x27;hello&#x27;</span>,<span class="string">&#x27;默认值&#x27;</span>))</span><br><span class="line">默认值</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;孙悟空&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d[<span class="string">&#x27;name&#x27;</span>] = <span class="string">&#x27;sunwukong&#x27;</span> <span class="comment"># 修改字典的key-value</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d[<span class="string">&#x27;address&#x27;</span>] = <span class="string">&#x27;花果山&#x27;</span> <span class="comment"># 向字典中添加key-value</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;sunwukong&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>, <span class="string">&#x27;address&#x27;</span>: <span class="string">&#x27;花果山&#x27;</span>&#125;</span><br></pre></td></tr></table></figure></li>
<li>setdefault(key[, default]): 可以用来向字典中添加key-value,<font color=red size=3><strong>不设置default默认为none</strong></font></li>
</ol>
<ul>
<li>如果key已经存在于字典中，则返回key的值，不会对字典做任何操作</li>
<li>如果key不存在，则向字典中添加这个key，并设置value<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#setdefault()</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;sunwukong&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>, <span class="string">&#x27;address&#x27;</span>: <span class="string">&#x27;花果山&#x27;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>result = d.setdefault(<span class="string">&#x27;name&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;sunwukong&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>, <span class="string">&#x27;address&#x27;</span>: <span class="string">&#x27;花果山&#x27;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>result</span><br><span class="line"><span class="string">&#x27;sunwukong&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>result = d.setdefault(<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>result</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;sunwukong&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>, <span class="string">&#x27;address&#x27;</span>: <span class="string">&#x27;花果山&#x27;</span>, <span class="string">&#x27;hello&#x27;</span>: <span class="literal">None</span>&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="9">
<li>update([other]): 将其他的字典中的key-value添加到当前字典中</li>
</ol>
<ul>
<li>如果有重复的key，则后边的会替换到当前的</li>
</ul>
<ol start="10">
<li>删除: 可以使用 del 来删除字典中的 key-value<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#update()</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d = &#123;<span class="string">&#x27;a&#x27;</span>:<span class="number">1</span>,<span class="string">&#x27;b&#x27;</span>:<span class="number">2</span>,<span class="string">&#x27;c&#x27;</span>:<span class="number">3</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d2 = &#123;<span class="string">&#x27;d&#x27;</span>:<span class="number">4</span>,<span class="string">&#x27;e&#x27;</span>:<span class="number">5</span>,<span class="string">&#x27;f&#x27;</span>:<span class="number">6</span>, <span class="string">&#x27;a&#x27;</span>:<span class="number">7</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d.update(d2)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">7</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;d&#x27;</span>: <span class="number">4</span>, <span class="string">&#x27;e&#x27;</span>: <span class="number">5</span>, <span class="string">&#x27;f&#x27;</span>: <span class="number">6</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#del 删除</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">7</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;d&#x27;</span>: <span class="number">4</span>, <span class="string">&#x27;e&#x27;</span>: <span class="number">5</span>, <span class="string">&#x27;f&#x27;</span>: <span class="number">6</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> d[<span class="string">&#x27;a&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> d[<span class="string">&#x27;b&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;d&#x27;</span>: <span class="number">4</span>, <span class="string">&#x27;e&#x27;</span>: <span class="number">5</span>, <span class="string">&#x27;f&#x27;</span>: <span class="number">6</span>&#125;</span><br></pre></td></tr></table></figure></li>
<li>popitem(): 随机删除字典中的一个键值对，一般都会删除最后一个键值对</li>
</ol>
<ul>
<li>删除之后，它会将删除的key-value作为返回值返回</li>
<li>返回的是一个元组，元组中有两个元素，第一个元素是删除的key，第二个是删除的value</li>
<li>当使用popitem()删除一个空字典时，会抛出异常 KeyError: ‘popitem(): dictionary is empty’</li>
</ul>
<ol start="12">
<li>pop(key[, default]): 根据key删除字典中的key-value,会将被删除的value返回！</li>
</ol>
<ul>
<li>如果删除不存在的key，会抛出异常。如果指定了默认值，再删除不存在的key时，不会报错，而是直接返回默认值<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#popitem() 随机删除字典中的一个键值对，一般删除最后一个键值对</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d</span><br><span class="line">&#123;<span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;d&#x27;</span>: <span class="number">4</span>, <span class="string">&#x27;e&#x27;</span>: <span class="number">5</span>, <span class="string">&#x27;f&#x27;</span>: <span class="number">6</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>result = d.popitem()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>result</span><br><span class="line">(<span class="string">&#x27;f&#x27;</span>, <span class="number">6</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#pop()，删除指定key</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d.pop(<span class="string">&#x27;d&#x27;</span>)</span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d.pop(<span class="string">&#x27;z&#x27;</span>,<span class="string">&#x27;这是默认值&#x27;</span>)</span><br><span class="line"><span class="string">&#x27;这是默认值&#x27;</span></span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="13">
<li>clear()用来清空字典, d.clear()</li>
<li>copy(): 该方法用于对字典进行浅复制</li>
</ol>
<ul>
<li>复制以后的对象，和原对象是独立，修改一个不会影响另一个</li>
<li>注意，<font color=red size=3><strong>浅复制会简单复制对象内部的值，如果值也是一个可变对象，这个可变对象不会被复制</strong></font><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>d = &#123;<span class="string">&#x27;a&#x27;</span>:<span class="number">1</span>,<span class="string">&#x27;b&#x27;</span>:<span class="number">2</span>,<span class="string">&#x27;c&#x27;</span>:<span class="number">3</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d2 = d  <span class="comment">#简单赋值，id一样</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;d= &#x27;</span>,d,<span class="built_in">id</span>(d))</span><br><span class="line">d=  &#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125; <span class="number">1962841154616</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;d2= &#x27;</span>,d2,<span class="built_in">id</span>(d2))</span><br><span class="line">d2=  &#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125; <span class="number">1962841154616</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d3 = d.copy() <span class="comment">#浅复制，id不同</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;d3= &#x27;</span>,d3,<span class="built_in">id</span>(d3))</span><br><span class="line">d3=  &#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125; <span class="number">1962841273400</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#解释如果值也是一个可变对象，这个可变对象不会被复制</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d = &#123;<span class="string">&#x27;a&#x27;</span>:&#123;<span class="string">&#x27;name&#x27;</span>:<span class="string">&#x27;孙悟空&#x27;</span>,<span class="string">&#x27;age&#x27;</span>:<span class="number">18</span>&#125;,<span class="string">&#x27;b&#x27;</span>:<span class="number">2</span>,<span class="string">&#x27;c&#x27;</span>:<span class="number">3</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d2 = d.copy()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d2[<span class="string">&#x27;a&#x27;</span>][<span class="string">&#x27;name&#x27;</span>] = <span class="string">&#x27;猪八戒&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;d = &#x27;</span>,d , <span class="built_in">id</span>(d))</span><br><span class="line">d =  &#123;<span class="string">&#x27;a&#x27;</span>: &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;猪八戒&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125; <span class="number">1962841155768</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;d2 = &#x27;</span>,d2 , <span class="built_in">id</span>(d2))</span><br><span class="line">d2 =  &#123;<span class="string">&#x27;a&#x27;</span>: &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;猪八戒&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125; <span class="number">1962841272680</span></span><br><span class="line"><span class="comment">#可以看到值包含一个可变对象用到的是引用其地址，</span></span><br><span class="line">所以对内部的可变对象赋值就会一起发生变化</span><br></pre></td></tr></table></figure>
<h2 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h2></li>
</ul>
<ol>
<li>集合和列表非常相似</li>
<li>不同点：</li>
</ol>
<ul>
<li>集合中只能存储不可变对象</li>
<li>集合中存储的对象是无序（不是按照元素的插入顺序保存）</li>
<li>集合中不能出现重复的元素<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用 &#123;&#125; 来创建集合</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = &#123;<span class="number">10</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">type</span>(s)</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">set</span>&#x27;&gt;</span></span><br><span class="line"><span class="class">&gt;&gt;&gt; <span class="title">s</span></span></span><br><span class="line">&#123;1, 2, 3, 5, 10&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#不能使用可变对象</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = &#123;[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],[<span class="number">4</span>,<span class="number">6</span>,<span class="number">7</span>]&#125;</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">TypeError: unhashable <span class="built_in">type</span>: <span class="string">&#x27;list&#x27;</span></span><br><span class="line"><span class="comment"># 使用 set() 函数来创建集合</span></span><br><span class="line">s = <span class="built_in">set</span>() <span class="comment"># 空集合</span></span><br><span class="line"><span class="comment"># 可以通过set()来将序列和字典转换为集合</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = <span class="built_in">set</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>])</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = <span class="built_in">set</span>(<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="string">&#x27;o&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;h&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用set()将字典转换为集合时，只会包含字典中的键</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = <span class="built_in">set</span>(&#123;<span class="string">&#x27;a&#x27;</span>:<span class="number">1</span>,<span class="string">&#x27;b&#x27;</span>:<span class="number">2</span>,<span class="string">&#x27;c&#x27;</span>:<span class="number">3</span>&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;b&#x27;</span>&#125;</span><br></pre></td></tr></table></figure>
<h3 id="使用集合"><a href="#使用集合" class="headerlink" title="使用集合"></a>使用集合</h3></li>
</ul>
<ol>
<li>使用in和not in来检查集合中的元素</li>
<li>使用len()来获取集合中元素的数量<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s = &#123;<span class="string">&#x27;a&#x27;</span> , <span class="string">&#x27;b&#x27;</span> , <span class="number">1</span> , <span class="number">2</span> , <span class="number">3</span> , <span class="number">1</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;c&#x27;</span> <span class="keyword">in</span> s)</span><br><span class="line"><span class="literal">False</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="built_in">len</span>(s))</span><br><span class="line"><span class="number">5</span></span><br></pre></td></tr></table></figure></li>
<li>add(): 向集合中添加元素<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&#x27;b&#x27;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.add(<span class="number">10</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.add(<span class="number">30</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">10</span>, <span class="number">30</span>&#125;</span><br></pre></td></tr></table></figure></li>
<li>update(): 将一个集合中的元素添加到当前集合中</li>
</ol>
<ul>
<li>update()可以传递序列或字典作为参数，字典只会使用键<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">10</span>, <span class="number">30</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s2 = <span class="built_in">set</span>(<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.update(s2)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">10</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="string">&#x27;h&#x27;</span>, <span class="number">30</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.update((<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>))</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">40</span>, <span class="number">10</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="number">50</span>, <span class="number">20</span>, <span class="string">&#x27;h&#x27;</span>, <span class="number">30</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.update(&#123;<span class="number">10</span>:<span class="string">&#x27;ab&#x27;</span>,<span class="number">20</span>:<span class="string">&#x27;bc&#x27;</span>,<span class="number">100</span>:<span class="string">&#x27;cd&#x27;</span>,<span class="number">1000</span>:<span class="string">&#x27;ef&#x27;</span>&#125;)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&#x27;l&#x27;</span>, <span class="number">100</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">40</span>, <span class="number">1000</span>, <span class="number">10</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="number">50</span>, <span class="number">20</span>, <span class="string">&#x27;h&#x27;</span>, <span class="number">30</span>&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="5">
<li>pop()随机删除并返回一个集合中的元素</li>
<li>remove()删除集合中的指定元素</li>
<li>clear()清空集合<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&#x27;l&#x27;</span>, <span class="number">100</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">40</span>, <span class="number">1000</span>, <span class="number">10</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="number">50</span>, <span class="number">20</span>, <span class="string">&#x27;h&#x27;</span>, <span class="number">30</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.pop()</span><br><span class="line"><span class="string">&#x27;a&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.remove(<span class="number">100</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line">&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">40</span>, <span class="number">1000</span>, <span class="number">10</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="number">50</span>, <span class="number">20</span>, <span class="string">&#x27;h&#x27;</span>, <span class="number">30</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s.clear()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s</span><br><span class="line"><span class="built_in">set</span>()</span><br></pre></td></tr></table></figure>
<h3 id="集合的运算"><a href="#集合的运算" class="headerlink" title="集合的运算"></a>集合的运算</h3><font color=red size=3><strong>在对集合做运算时，不会影响原来的集合，而是返回一个运算结果</strong></font><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建两个集合</span></span><br><span class="line">s = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;</span><br><span class="line">s2 = &#123;<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># &amp; 交集运算</span></span><br><span class="line">result = s &amp; s2 <span class="comment"># &#123;3, 4, 5&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># | 并集运算</span></span><br><span class="line">result = s | s2 <span class="comment"># &#123;1,2,3,4,5,6,7&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># - 差集</span></span><br><span class="line">result = s - s2 <span class="comment"># &#123;1, 2&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># ^ 异或集 获取只在一个集合中出现的元素</span></span><br><span class="line">result = s ^ s2 <span class="comment"># &#123;1, 2, 6, 7&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># &lt;= 检查一个集合是否是另一个集合的子集</span></span><br><span class="line"><span class="comment"># 如果a集合中的元素全部都在b集合中出现，那么a集合就是b集合的子集，b集合是a集合超集</span></span><br><span class="line">a = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;</span><br><span class="line">b = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;</span><br><span class="line"></span><br><span class="line">result = a &lt;= b <span class="comment"># True</span></span><br><span class="line">result = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125; &lt;= &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125; <span class="comment"># True</span></span><br><span class="line">result = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125; &lt;= &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125; <span class="comment"># False</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># &lt; 检查一个集合是否是另一个集合的真子集</span></span><br><span class="line"><span class="comment"># 如果超集b中含有子集a中所有元素，并且b中还有a中没有的元素，则b就是a的真超集，a是b的真子集</span></span><br><span class="line">result = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125; &lt; &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125; <span class="comment"># False</span></span><br><span class="line">result = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125; &lt; &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125; <span class="comment"># True</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># &gt;= 检查一个集合是否是另一个的超集</span></span><br><span class="line"><span class="comment"># &gt; 检查一个集合是否是另一个的真超集</span></span><br><span class="line">print(<span class="string">&#x27;result =&#x27;</span>,result)</span><br></pre></td></tr></table></figure>
<h1 id="第六章-函数"><a href="#第六章-函数" class="headerlink" title="第六章 函数"></a>第六章 函数</h1><h2 id="函数简介（function）"><a href="#函数简介（function）" class="headerlink" title="函数简介（function）"></a>函数简介（function）</h2></li>
<li>函数也是一个对象</li>
<li>对象是内存中专门用来存储数据的一块区域</li>
<li>函数可以用来保存一些可执行的代码，并且可以在需要时，对这些语句进行多次的调用</li>
<li>创建函数：<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> 函数名(<span class="params">[形参<span class="number">1</span>,形参<span class="number">2</span>,...形参n]</span>) :</span></span><br><span class="line">    代码块</span><br><span class="line">    </span><br><span class="line"><span class="comment"># 定义一个函数</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn</span>() :</span></span><br><span class="line">    print(<span class="string">&#x27;这是我的第一个函数！&#x27;</span>)</span><br><span class="line">    print(<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line">    print(<span class="string">&#x27;今天天气真不错！&#x27;</span>)</span><br></pre></td></tr></table></figure></li>
</ol>
<ul>
<li>函数名必须要符号标识符的规范<ul>
<li>（可以包含字母、数字、下划线、但是不能以数字开头）    </li>
</ul>
</li>
</ul>
<ol start="5">
<li>函数中保存的代码不会立即执行，需要调用函数代码才会执行</li>
<li>调用函数：</li>
</ol>
<ul>
<li>函数对象()<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 打印fn</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(fn)</span><br><span class="line">&lt;function fn at <span class="number">0x000001C9023C2EA0</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="built_in">type</span>(fn))</span><br><span class="line">&lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">function</span>&#x27;&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"># <span class="title">fn</span>是函数对象  <span class="title">fn</span>()调用函数</span></span><br><span class="line"><span class="class"># <span class="title">print</span>是函数对象 <span class="title">print</span>()调用函数</span></span><br><span class="line"><span class="class">&gt;&gt;&gt; <span class="title">fn</span>()</span></span><br><span class="line"><span class="class">这是我的第一个函数!</span></span><br><span class="line"><span class="class"><span class="title">hello</span></span></span><br><span class="line"><span class="class">今天天气真不错!</span></span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="7">
<li>定义函数一般都是要实现某种功能的    </li>
</ol>
<h2 id="函数的参数"><a href="#函数的参数" class="headerlink" title="函数的参数"></a>函数的参数</h2><ol>
<li>在定义函数时，可以在函数名后的()中定义数量不等的形参，</li>
</ol>
<ul>
<li>多个形参之间使用,隔开</li>
</ul>
<ol start="2">
<li>形参（形式参数），定义形参就相当于在函数内部声明了变量，但是并不赋值</li>
<li>实参（实际参数）</li>
</ol>
<ul>
<li>如果函数定义时，指定了形参，那么在调用函数时也必须传递实参，<ul>
<li>实参将会赋值给对应的形参，简单来说，有几个形参就得传几个实参<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># 定义函数时指定形参</span></span><br><span class="line"><span class="meta">... </span><span class="function"><span class="keyword">def</span> <span class="title">fn2</span>(<span class="params">a , b</span>) :</span></span><br><span class="line"><span class="meta">... </span>    <span class="comment"># print(&#x27;a =&#x27;,a)</span></span><br><span class="line"><span class="meta">... </span>    <span class="comment"># print(&#x27;b =&#x27;,b)</span></span><br><span class="line"><span class="meta">... </span>    print(a,<span class="string">&quot;+&quot;</span>,b,<span class="string">&quot;=&quot;</span>,a + b)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># 调用函数时，来传递实参</span></span><br><span class="line"><span class="meta">... </span>fn2(<span class="number">10</span>,<span class="number">20</span>)</span><br><span class="line"><span class="number">10</span> + <span class="number">20</span> = <span class="number">30</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn2(<span class="number">123</span>,<span class="number">456</span>)</span><br><span class="line"><span class="number">123</span> + <span class="number">456</span> = <span class="number">579</span></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<ol start="4">
<li>默认形参：指定了默认值以后，如果用户传递了参数则默认值没有任何作用</li>
</ol>
<ul>
<li>默认参数必须在无默认值的参数后面<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn</span>(<span class="params">a = <span class="number">5</span> , b = <span class="number">10</span> , c</span>):</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;a =&#x27;</span>,a)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;b= &#x27;</span>,b)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;c= &#x27;</span>,c)</span><br><span class="line">...</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span></span><br><span class="line">SyntaxError: non-default argument follows default argument</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn</span>(<span class="params">a , b = <span class="number">10</span> , c = <span class="number">20</span></span>):</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;a =&#x27;</span>,a)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;b= &#x27;</span>,b)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;c= &#x27;</span>,c)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn(<span class="number">2</span>)</span><br><span class="line">a = <span class="number">2</span></span><br><span class="line">b=  <span class="number">10</span></span><br><span class="line">c=  <span class="number">20</span></span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="5">
<li>位置参数和关键字参数：混合使用关键字和位置参数时，必须将位置参数写到前面<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn</span>(<span class="params">a , b = <span class="number">10</span> , c = <span class="number">20</span></span>):</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;a =&#x27;</span>,a)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;b= &#x27;</span>,b)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;c= &#x27;</span>,c)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn(<span class="number">2</span>,c = <span class="number">30</span>)</span><br><span class="line">a = <span class="number">2</span></span><br><span class="line">b=  <span class="number">10</span></span><br><span class="line">c=  <span class="number">30</span></span><br></pre></td></tr></table></figure></li>
<li> 函数在调用时，解析器不会检查实参的类型。实参可以传递任意类型的对象</li>
<li>形参传入的是一个对象，当通过形参去修改对象时会影响到所有指向该对象的变量<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn4</span>(<span class="params">a</span>):</span></span><br><span class="line"><span class="meta">... </span>    a[<span class="number">0</span>] = <span class="number">30</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;a =&#x27;</span>,a,<span class="built_in">id</span>(a))</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>c = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn4(c)</span><br><span class="line">a = [<span class="number">30</span>, <span class="number">2</span>, <span class="number">3</span>] <span class="number">1962841246856</span></span><br><span class="line"><span class="comment">#传入的是列表c的一个浅复制，并不会影响列表C中的值</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn4(c.copy())   </span><br><span class="line">a = [<span class="number">30</span>, <span class="number">2</span>, <span class="number">3</span>] <span class="number">1962841286408</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#和以上一样，切片没指定开始和结束，即是浅复制</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn4(c[:])</span><br><span class="line">a = [<span class="number">30</span>, <span class="number">2</span>, <span class="number">3</span>] <span class="number">1962841286408</span></span><br></pre></td></tr></table></figure>
<h2 id="不定长的参数"><a href="#不定长的参数" class="headerlink" title="不定长的参数"></a>不定长的参数</h2><div class="note primary flat"><p>在形参前边加上一个*，这样这个形参将会获取到所有的实参，<br>本质上是将获得的实参保存为一个元组，<font color=red size=3><strong>参考元组那一小节的赋值</strong></font></p>
</div>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">sum</span>(<span class="params">*nums</span>):</span></span><br><span class="line"><span class="meta">... </span>    result = <span class="number">0</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">for</span> n <span class="keyword">in</span> nums :</span><br><span class="line"><span class="meta">... </span>        result += n</span><br><span class="line"><span class="meta">... </span>    print(result)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">sum</span>(<span class="number">123</span>,<span class="number">456</span>,<span class="number">789</span>,<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>)</span><br><span class="line"><span class="number">1468</span></span><br></pre></td></tr></table></figure></li>
<li>可变参数不是必须写在最后，但是注意，带*的参数后的所有参数，必须以关键字参数的形式传递<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 第一个参数给a，剩下的位置参数给b的元组，c必须使用关键字参数</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn2</span>(<span class="params">a,*b,c</span>):</span></span><br><span class="line">    print(<span class="string">&#x27;a =&#x27;</span>,a)</span><br><span class="line">    print(<span class="string">&#x27;b =&#x27;</span>,b)</span><br><span class="line">    print(<span class="string">&#x27;c =&#x27;</span>,c)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn2(<span class="number">23</span>,<span class="number">453</span>,<span class="number">54</span>,c=<span class="number">21</span>,<span class="number">65</span>)</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span></span><br><span class="line">SyntaxError: positional argument follows keyword argument</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn2(<span class="number">23</span>,<span class="number">453</span>,<span class="number">54</span>,c=<span class="number">21</span>)</span><br><span class="line">a = <span class="number">23</span></span><br><span class="line">b = (<span class="number">453</span>, <span class="number">54</span>)</span><br><span class="line">c = <span class="number">21</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn2(<span class="number">23</span>,<span class="number">453</span>,<span class="number">54</span>)</span><br><span class="line">Traceback (most recent call last):</span><br><span class="line">  File <span class="string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</span><br><span class="line">TypeError: fn2() missing <span class="number">1</span> required keyword-only argument: <span class="string">&#x27;c&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 所有的位置参数都给a，b和c必须使用关键字参数</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn2</span>(<span class="params">*a,b,c</span>):</span></span><br><span class="line">    print(<span class="string">&#x27;a =&#x27;</span>,a)</span><br><span class="line">    print(<span class="string">&#x27;b =&#x27;</span>,b)</span><br><span class="line">    print(<span class="string">&#x27;c =&#x27;</span>,c)</span><br><span class="line">    </span><br><span class="line"><span class="comment"># 如果在形参的开头直接写一个*,则要求我们的所有的参数必须以关键字参数的形式传递</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn2</span>(<span class="params">*,a,b,c</span>):</span></span><br><span class="line">    print(<span class="string">&#x27;a =&#x27;</span>,a)</span><br><span class="line">    print(<span class="string">&#x27;b =&#x27;</span>,b)</span><br><span class="line">    print(<span class="string">&#x27;c =&#x27;</span>,c)</span><br></pre></td></tr></table></figure></li>
<li>*形参只能接收位置参数，而不能接收关键字参数<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn3</span>(<span class="params">*a</span>) :</span></span><br><span class="line">    print(<span class="string">&#x27;a =&#x27;</span>,a)</span><br></pre></td></tr></table></figure></li>
<li>**形参可以接收其他的关键字参数，它会将这些参数统一保存到一个字典中</li>
</ol>
<ul>
<li>字典的key就是参数的名字，字典的value就是参数的值</li>
<li>**形参只能有一个，并且必须写在所有参数的最后<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn3</span>(<span class="params">b,c,**a</span>) :</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;a =&#x27;</span>,a,<span class="built_in">type</span>(a))</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;b =&#x27;</span>,b)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;c =&#x27;</span>,c)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn3(b=<span class="number">1</span>,d=<span class="number">2</span>,c=<span class="number">3</span>,e=<span class="number">10</span>,f=<span class="number">20</span>)</span><br><span class="line">a = &#123;<span class="string">&#x27;d&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;e&#x27;</span>: <span class="number">10</span>, <span class="string">&#x27;f&#x27;</span>: <span class="number">20</span>&#125; &lt;<span class="class"><span class="keyword">class</span> &#x27;<span class="title">dict</span>&#x27;&gt;</span></span><br><span class="line">b = 1</span><br><span class="line">c = <span class="number">3</span></span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="4">
<li>参数的解包（拆包）</li>
</ol>
<ul>
<li>传递实参时，也可以在序列类型的参数前添加星号，这样他会自动将序列中的元素依次作为参数传递</li>
<li>要求序列中元素的个数必须和形参的个数的一致<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn4</span>(<span class="params">a,b,c</span>):</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;a =&#x27;</span>,a)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;b =&#x27;</span>,b)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;c =&#x27;</span>,c)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>t = (<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn4(*t)</span><br><span class="line">a = <span class="number">10</span></span><br><span class="line">b = <span class="number">20</span></span><br><span class="line">c = <span class="number">30</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn4</span>(<span class="params">a,b,*c</span>):</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;a =&#x27;</span>,a)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;b =&#x27;</span>,b)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;c =&#x27;</span>,c)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>t = (<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn4(*t)</span><br><span class="line">a = <span class="number">10</span></span><br><span class="line">b = <span class="number">20</span></span><br><span class="line">c = (<span class="number">30</span>, <span class="number">40</span>)</span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="5">
<li>通过 **来对一个字典进行解包操作<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn4</span>(<span class="params">a,b,c</span>):</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;a =&#x27;</span>,a)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;b =&#x27;</span>,b)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;c =&#x27;</span>,c)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>d = &#123;<span class="string">&#x27;a&#x27;</span>:<span class="number">100</span>,<span class="string">&#x27;b&#x27;</span>:<span class="number">200</span>,<span class="string">&#x27;c&#x27;</span>:(<span class="number">300</span>,<span class="number">400</span>)&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn4(**d)</span><br><span class="line">a = <span class="number">100</span></span><br><span class="line">b = <span class="number">200</span></span><br><span class="line">c = (<span class="number">300</span>, <span class="number">400</span>)</span><br></pre></td></tr></table></figure>
<h2 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h2></li>
<li>关键字：return</li>
<li>return 可以返回一个函数<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn</span>():</span></span><br><span class="line"><span class="meta">... </span>    <span class="function"><span class="keyword">def</span> <span class="title">fn2</span>():</span></span><br><span class="line"><span class="meta">... </span>            print(<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line"><span class="meta">... </span>    <span class="keyword">return</span> fn2</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>r = fn() <span class="comment">#使用变量保存一个函数对象，fn返回的是fn2这个函数对象</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>r()</span><br><span class="line">hello</span><br></pre></td></tr></table></figure></li>
<li>如果仅仅写一个return 或者 不写return，则相当于return None </li>
</ol>
<h2 id="文档字符串"><a href="#文档字符串" class="headerlink" title="文档字符串"></a>文档字符串</h2><ol>
<li>help()是Python中的内置函数</li>
<li>通过help()函数可以查询python中的函数的用法</li>
</ol>
<ul>
<li>语法：help(函数对象)<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">help</span>(<span class="built_in">print</span>) <span class="comment"># 获取print()函数的使用说明</span></span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="3">
<li>在定义函数时，可以在函数内部编写文档字符串，文档字符串就是函数的说明</li>
<li>当我们编写了文档字符串时，就可以通过help()函数来查看函数的说明<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#对形参进行类型描述只是作为文档描述，实际不起作用，可以传入任何类型</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn</span>(<span class="params">a:<span class="built_in">int</span>,b:<span class="built_in">bool</span>,c:<span class="built_in">str</span>=<span class="string">&#x27;hello&#x27;</span></span>) -&gt; int:</span></span><br><span class="line"><span class="meta">... </span>    <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string"><span class="meta">... </span>    这是一个文档字符串的示例</span></span><br><span class="line"><span class="string">...</span></span><br><span class="line"><span class="string"><span class="meta">... </span>    函数的作用：。。。。。</span></span><br><span class="line"><span class="string"><span class="meta">... </span>    函数的参数：</span></span><br><span class="line"><span class="string"><span class="meta">... </span>        a，作用，类型，默认值。。。。</span></span><br><span class="line"><span class="string"><span class="meta">... </span>        b，作用，类型，默认值。。。。</span></span><br><span class="line"><span class="string"><span class="meta">... </span>        c，作用，类型，默认值。。。。</span></span><br><span class="line"><span class="string"><span class="meta">... </span>    &#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">return</span> <span class="number">10</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">help</span>(fn)</span><br><span class="line">Help on function fn <span class="keyword">in</span> module __main__:</span><br><span class="line"></span><br><span class="line">fn(a:int, b:bool, c:str=&#x27;hello&#x27;) -&gt; int</span><br><span class="line">    这是一个文档字符串的示例</span><br><span class="line"></span><br><span class="line">    函数的作用：。。。。。</span><br><span class="line">    函数的参数：</span><br><span class="line">        a，作用，类型，默认值。。。。</span><br><span class="line">        b，作用，类型，默认值。。。。</span><br><span class="line">        c，作用，类型，默认值。。。。</span><br></pre></td></tr></table></figure>
<h2 id="作用域与命名空间"><a href="#作用域与命名空间" class="headerlink" title="作用域与命名空间"></a>作用域与命名空间</h2><h3 id="作用域"><a href="#作用域" class="headerlink" title="作用域"></a>作用域</h3></li>
<li>使用变量时，会优先在当前作用域中寻找该变量，如果有则使用<br>如果没有则继续去上一级作用域中寻找,以此类推</li>
<li>在函数中为变量赋值时，默认都是为局部变量赋值<br>如果希望在函数内部修改全局变量，则需要使用global关键字，来声明变量<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="number">20</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">fn3</span>():</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">global</span> a    <span class="comment">#必须且只能是声明，不能直接赋值</span></span><br><span class="line"><span class="meta">... </span>    a = <span class="number">10</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&#x27;函数内部: &#x27;</span>,<span class="string">&#x27;a = &#x27;</span>,a)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn3()</span><br><span class="line">函数内部:  a =  <span class="number">10</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line"><span class="number">10</span></span><br></pre></td></tr></table></figure>
<h3 id="命名空间"><a href="#命名空间" class="headerlink" title="命名空间"></a>命名空间</h3></li>
<li>locals(): 获取当前命名空间</li>
<li>globals(): 在任意位置获取全局命名空间</li>
<li>不建议通过命名空间来修改变量值</li>
</ol>
<h2 id="递归"><a href="#递归" class="headerlink" title="递归"></a>递归</h2><ol>
<li>递归是解决问题的一种方式，它和循环很像</li>
</ol>
<ul>
<li>它的整体思想是，将一个大问题分解为一个个的小问题，直到问题无法分解时，再去解决问题</li>
</ul>
<ol start="2">
<li>递归式函数的两个要件</li>
</ol>
<ul>
<li>基线条件<ul>
<li>问题可以被分解为的最小问题，当满足基线条件时，递归就不在执行了</li>
</ul>
</li>
<li>递归条件<ul>
<li>将问题继续分解的条件<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">factorial</span>(<span class="params">n</span>):</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">if</span> n == <span class="number">1</span>:</span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> <span class="number">1</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">return</span> n * factorial(n-<span class="number">1</span>)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>factorial(<span class="number">5</span>)</span><br><span class="line"><span class="number">120</span></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<ol start="3">
<li>递归深度: python3默认递归的深度为100层，可以修改递归深度<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> sys</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>sys.setrecursionlimit(<span class="number">1000000</span>)      //设置递归的深度为<span class="number">100</span>万层</span><br></pre></td></tr></table></figure>
<h1 id="第七章-高阶函数"><a href="#第七章-高阶函数" class="headerlink" title="第七章 高阶函数"></a>第七章 高阶函数</h1><h2 id="高阶函数"><a href="#高阶函数" class="headerlink" title="高阶函数"></a>高阶函数</h2></li>
<li>接收函数作为参数，或者将函数作为返回值的函数是高阶函数<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn</span>(<span class="params">func, lst</span>):</span></span><br><span class="line">    new_list = []</span><br><span class="line">    <span class="keyword">for</span> n <span class="keyword">in</span> lst :</span><br><span class="line">            <span class="keyword">if</span> func(n):</span><br><span class="line">                new_list.append(n)</span><br><span class="line">    <span class="keyword">return</span> new_list</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn4</span>(<span class="params">i</span>):</span></span><br><span class="line">    <span class="keyword">return</span> i % <span class="number">3</span> == <span class="number">0</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">l = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>]</span><br><span class="line"></span><br><span class="line">print(fn(fn4, l))   <span class="comment">#[3, 6, 9]</span></span><br></pre></td></tr></table></figure></li>
<li>filter(): 可以从序列中过滤出符合条件的元素，保存到一个新的序列中</li>
</ol>
<ul>
<li>参数：<ul>
<li>1.函数，根据该函数来过滤序列（可迭代的结构）</li>
<li>2.需要过滤的序列（可迭代的结构）</li>
</ul>
</li>
<li>返回值：<ul>
<li>过滤后的新序列（可迭代的结构）</li>
</ul>
</li>
</ul>
<ol start="3">
<li>匿名函数 lambda 函数表达式 （语法糖）</li>
</ol>
<ul>
<li>lambda函数表达式专门用来创建一些简单的函数，他是函数创建的又一种方式</li>
<li>语法：lambda 参数列表 : 返回值</li>
<li>匿名函数一般都是作为参数使用，其他地方一般不会使用</li>
<li>可以将匿名函数赋值给一个变量，一般不会这么做<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>fn6 = <span class="keyword">lambda</span> a,b : a + b</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(fn6(<span class="number">10</span>,<span class="number">30</span>))</span><br><span class="line"><span class="number">40</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>l = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>r = <span class="built_in">filter</span>(<span class="keyword">lambda</span> i : i &gt; <span class="number">5</span> , l)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>r</span><br><span class="line">&lt;<span class="built_in">filter</span> <span class="built_in">object</span> at <span class="number">0x000001C902B049E8</span>&gt;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="built_in">list</span>(r))</span><br><span class="line">[<span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>]</span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="4">
<li>map(): 可以对可迭代对象中的所有元素做指定的操作，然后将其添加到一个新的对象中返回<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>l = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>]</span><br><span class="line"><span class="comment"># 对列表中的每个元素进行指数操作</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>r = <span class="built_in">map</span>(<span class="keyword">lambda</span> i : i ** <span class="number">2</span> , l)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="built_in">list</span>(r))</span><br><span class="line">[<span class="number">1</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">16</span>, <span class="number">25</span>, <span class="number">36</span>, <span class="number">49</span>, <span class="number">64</span>, <span class="number">81</span>, <span class="number">100</span>]</span><br></pre></td></tr></table></figure></li>
<li>sort(): 该方法用来对列表中的元素进行排序,默认是直接比较列表中的元素的大小</li>
</ol>
<ul>
<li>在sort()可以接收一个关键字参数 ， key<ul>
<li>key需要一个函数作为参数，当设置了函数作为参数</li>
<li>每次都会以列表中的一个元素作为参数来调用函数，并且使用函数的返回值来比较元素的大小<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>l = [<span class="string">&#x27;bb&#x27;</span>,<span class="string">&#x27;aaaa&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;ddddddddd&#x27;</span>,<span class="string">&#x27;fff&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>l.sort(key=<span class="built_in">len</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>l</span><br><span class="line">[<span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;bb&#x27;</span>, <span class="string">&#x27;fff&#x27;</span>, <span class="string">&#x27;aaaa&#x27;</span>, <span class="string">&#x27;ddddddddd&#x27;</span>]</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<ol start="6">
<li>sorted(): 这个函数和sort()的用法基本一致，但是sorted()可以对任意的序列进行排序</li>
</ol>
<ul>
<li>并且使用sorted()排序不会影响原来的对象，而是返回一个新对象<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>l = [<span class="number">2</span>,<span class="number">5</span>,<span class="string">&#x27;1&#x27;</span>,<span class="number">3</span>,<span class="string">&#x27;6&#x27;</span>,<span class="string">&#x27;4&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;排序前:&#x27;</span>,l)</span><br><span class="line">排序前: [<span class="number">2</span>, <span class="number">5</span>, <span class="string">&#x27;1&#x27;</span>, <span class="number">3</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;4&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="built_in">sorted</span>(l,key=<span class="built_in">int</span>))</span><br><span class="line">[<span class="string">&#x27;1&#x27;</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="string">&#x27;4&#x27;</span>, <span class="number">5</span>, <span class="string">&#x27;6&#x27;</span>]</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">&#x27;排序后:&#x27;</span>,l)</span><br><span class="line">排序后: [<span class="number">2</span>, <span class="number">5</span>, <span class="string">&#x27;1&#x27;</span>, <span class="number">3</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;4&#x27;</span>]</span><br></pre></td></tr></table></figure>
<h2 id="闭包"><a href="#闭包" class="headerlink" title="闭包"></a>闭包</h2></li>
</ul>
<ol>
<li>将函数作为返回值返回，也是一种高阶函数</li>
<li>这种高阶函数我们也称为叫做闭包，通过闭包可以创建一些只有当前函数能访问的变量</li>
<li>可以将一些私有的数据藏到的闭包中</li>
<li>形成闭包的条件</li>
</ol>
<ul>
<li>函数嵌套</li>
<li>将内部函数作为返回值返回</li>
<li>内部函数必须要使用到外部函数的变量<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">make_averager</span>():</span></span><br><span class="line">     nums = []</span><br><span class="line">     <span class="function"><span class="keyword">def</span> <span class="title">averager</span>(<span class="params">n</span>):</span></span><br><span class="line">             nums.append(n)</span><br><span class="line">             <span class="keyword">return</span> <span class="built_in">sum</span>(nums)/<span class="built_in">len</span>(nums)</span><br><span class="line">     <span class="keyword">return</span> averager</span><br><span class="line"></span><br><span class="line">averager = make_averager()</span><br><span class="line">print(averager(<span class="number">10</span>)) <span class="comment">#10.0</span></span><br></pre></td></tr></table></figure>
<h2 id="装饰器"><a href="#装饰器" class="headerlink" title="装饰器"></a>装饰器</h2></li>
</ul>
<ol>
<li>开闭原则（OCP）: 程序的设计，要求开发对程序的扩展，要关闭对程序的修改<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">add</span>(<span class="params">a, b</span>):</span></span><br><span class="line">    r = a + b</span><br><span class="line">    <span class="keyword">return</span> r</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">begin_end</span>(<span class="params">old</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">new_function</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;开始执行~~~~&#x27;</span>)</span><br><span class="line">        result = old(*args, **kwargs)</span><br><span class="line">        print(<span class="string">&#x27;执行结束~~~~&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line">    <span class="keyword">return</span> new_function</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">f = begin_end(add)</span><br><span class="line">t = f(<span class="number">123</span>, <span class="number">456</span>)</span><br><span class="line">print(t)</span><br><span class="line"></span><br><span class="line"><span class="comment">#开始执行~~~~</span></span><br><span class="line"><span class="comment">#执行结束~~~~</span></span><br><span class="line"><span class="comment">#579#</span></span><br></pre></td></tr></table></figure></li>
<li>向begin_end()这种函数我们就称它为装饰器</li>
</ol>
<ul>
<li>通过装饰器，可以在不修改原来函数的情况下来对函数进行扩展</li>
<li>在开发中，我们都是通过装饰器来扩展函数的功能的</li>
<li>在定义函数时，可以通过@装饰器，来使用指定的装饰器，来装饰当前的函数</li>
<li>可以同时为一个函数指定多个装饰器，这样函数将会安装从内向外的顺序被装饰 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">begin_end</span>(<span class="params">old</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">new_function</span>(<span class="params">*args, **kwargs</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;开始执行~~~~&#x27;</span>)</span><br><span class="line">        result = old(*args, **kwargs)</span><br><span class="line">        print(<span class="string">&#x27;执行结束~~~~&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line">    <span class="keyword">return</span> new_function</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fn3</span>(<span class="params">old</span>):</span></span><br><span class="line">    <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">        用来对其他函数进行扩展，使其他函数可以在执行前打印开始执行，执行后打印执行结束</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        参数：</span></span><br><span class="line"><span class="string">            old 要扩展的函数对象</span></span><br><span class="line"><span class="string">    &#x27;&#x27;&#x27;</span></span><br><span class="line"> 09        print(<span class="string">&#x27;fn3装饰~开始执行~~~~&#x27;</span>)</span><br><span class="line">        <span class="comment"># 调用被扩展的函数</span></span><br><span class="line">        result = old(*args , **kwargs)</span><br><span class="line">        print(<span class="string">&#x27;fn3装饰~执行结束~~~~&#x27;</span>)</span><br><span class="line">        <span class="comment"># 返回函数的执行结果</span></span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 返回新函数</span></span><br><span class="line">    <span class="keyword">return</span> new_function</span><br><span class="line"></span><br><span class="line"><span class="meta">@fn3</span></span><br><span class="line"><span class="meta">@begin_end</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">say_hello</span>():</span></span><br><span class="line">    print(<span class="string">&#x27;大家好~~~&#x27;</span>)</span><br><span class="line"></span><br><span class="line">say_hello()</span><br><span class="line"></span><br><span class="line"><span class="comment">#打印结果</span></span><br><span class="line">fn3装饰~开始执行~~~~</span><br><span class="line">开始执行~~~~</span><br><span class="line">大家好~~~</span><br><span class="line">执行结束~~~~</span><br><span class="line">fn3装饰~执行结束~~~~</span><br></pre></td></tr></table></figure>
<h1 id="第八章-对象（Object）"><a href="#第八章-对象（Object）" class="headerlink" title="第八章 对象（Object）"></a>第八章 对象（Object）</h1><h2 id="什么是对象？"><a href="#什么是对象？" class="headerlink" title="什么是对象？"></a>什么是对象？</h2></li>
</ul>
<ol>
<li>对象是内存中专门用来存储数据的一块区域。</li>
<li>对象中可以存放各种数据（比如：数字、布尔值、代码）</li>
<li>对象由三部分组成：</li>
</ol>
<ul>
<li>对象的标识（id）</li>
<li>对象的类型（type）</li>
<li>对象的值（value）</li>
</ul>
<h2 id="类-class"><a href="#类-class" class="headerlink" title="类(class)"></a>类(class)</h2><ul>
<li>我们目前所学习的对象都是Python内置的对象</li>
<li>但是内置对象并不能满足所有的需求，所以我们在开发中经常需要自定义一些对象</li>
<li>类，简单理解它就相当于一个图纸。在程序中我们需要根据类来创建对象</li>
<li>类就是对象的图纸！</li>
<li>我们也称对象是类的实例（instance）</li>
<li>如果多个对象是通过一个类创建的，我们称这些对象是一类对象</li>
<li>像 int() float() bool() str() list() dict() …. 这些都是类</li>
<li>a = int(10) # 创建一个int类的实例 等价于 a = 10</li>
<li>我们自定义的类都需要使用大写字母开头，使用大驼峰命名法（帕斯卡命名法）来对类命名</li>
<li>类也是一个对象！</li>
<li>类就是一个用来创建对象的对象！</li>
<li>类是type类型的对象，定义类实际上就是定义了一个type类型的对象</li>
</ul>
<h3 id="类的简单使用"><a href="#类的简单使用" class="headerlink" title="类的简单使用"></a>类的简单使用</h3><ol>
<li>isinstance(): 用来检查一个对象是否是一个类的实例<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#定义一个类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyClass</span>():</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line">    </span><br><span class="line"><span class="comment"># 使用类来创建对象，就像调用一个函数一样</span></span><br><span class="line">mc = MyClass() <span class="comment"># mc就是通过MyClass创建的对象，mc是MyClass的实例</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#检查mc对象是否是MyClass类的一个实例</span></span><br><span class="line">result = <span class="built_in">isinstance</span>(mc,MyClass)</span><br><span class="line"></span><br><span class="line"><span class="comment">#现在通过MyClass这个类创建的对象都是一个空对象，</span></span><br><span class="line"><span class="comment">#可以向对象中添加变量，对象中的变量称为属性</span></span><br><span class="line"><span class="comment">#语法：对象.属性名 = 属性值</span></span><br><span class="line">mc.name = <span class="string">&#x27;孙悟空&#x27;</span></span><br><span class="line"></span><br><span class="line">print(mc.name)  <span class="comment">#孙悟空</span></span><br></pre></td></tr></table></figure>
<h3 id="类的定义"><a href="#类的定义" class="headerlink" title="类的定义"></a>类的定义</h3></li>
<li>类对象和实例对象中都可以保存属性（方法）</li>
</ol>
<ul>
<li>如果这个属性（方法）是所有的实例共享的，则应该将其保存到类对象中</li>
<li>如果这个属性（方法）是某个实例独有，则应该保存到实例对象中     </li>
</ul>
<ol start="2">
<li>一般情况下，属性保存到实例对象中</li>
</ol>
<ul>
<li>而方法需要保存到类对象中    <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 尝试定义一个表示人的类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> :</span></span><br><span class="line">    <span class="comment"># 在类的代码块中，我们可以定义变量和函数</span></span><br><span class="line">    <span class="comment"># 在类中我们所定义的变量，将会成为所有的实例的公共属性</span></span><br><span class="line">    <span class="comment"># 所有实例都可以访问这些变量</span></span><br><span class="line">    name = <span class="string">&#x27;swk&#x27;</span> <span class="comment"># 公共属性，所有实例都可以访问</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 在类中也可以定义函数，类中的定义的函数，我们称为方法</span></span><br><span class="line">    <span class="comment"># 这些方法可以通过该类的所有实例来访问</span></span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">say_hello</span>(<span class="params">self</span>) :</span></span><br><span class="line">        <span class="comment"># 方法每次被调用时，解析器都会自动传递第一个实参</span></span><br><span class="line">        <span class="comment"># 第一个参数，就是调用方法的对象本身，</span></span><br><span class="line">        <span class="comment">#   如果是p1调的，则第一个参数就是p1对象</span></span><br><span class="line">        <span class="comment">#   如果是p2调的，则第一个参数就是p2对象</span></span><br><span class="line">        <span class="comment"># 一般我们都会将这个参数命名为self</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># say_hello()这个方法，可以显示如下格式的数据：</span></span><br><span class="line">        <span class="comment">#   你好！我是 xxx</span></span><br><span class="line">        <span class="comment">#   在方法中不能直接访问类中的属性</span></span><br><span class="line">        print(<span class="string">&#x27;你好！我是 %s&#x27;</span> %self.name)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建Person的实例</span></span><br><span class="line">p1 = Person()</span><br><span class="line">p2 = Person()</span><br><span class="line"></span><br><span class="line"><span class="comment"># print(p2.name)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用方法，对象.方法名()</span></span><br><span class="line"><span class="comment"># 方法调用和函数调用的区别</span></span><br><span class="line"><span class="comment"># 如果是函数调用，则调用时传几个参数，就会有几个实参</span></span><br><span class="line"><span class="comment"># 但是如果是方法调用，默认传递一个参数，所以方法中至少要定义一个形参</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 修改p1的name属性</span></span><br><span class="line">p1.name = <span class="string">&#x27;猪八戒&#x27;</span></span><br><span class="line">p2.name = <span class="string">&#x27;沙和尚&#x27;</span></span><br><span class="line"></span><br><span class="line">p1.say_hello() <span class="comment"># &#x27;你好！我是 猪八戒&#x27;</span></span><br><span class="line">p2.say_hello() <span class="comment"># &#x27;你好！我是 沙和尚&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># del p2.name # 删除p2的name属性</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># print(p1.name) # swk</span></span><br><span class="line"><span class="comment"># print(p2.name) # 沙和尚</span></span><br></pre></td></tr></table></figure>
<h3 id="特殊方法"><a href="#特殊方法" class="headerlink" title="特殊方法"></a>特殊方法</h3></li>
</ul>
<ol>
<li>在类中可以定义一些特殊方法（魔术方法）</li>
<li>特殊方法都是以__开头，__结尾的方法【<font color=red size=3><strong>双下划线</strong></font>】</li>
<li>特殊方法不需要我们自己调用，不要尝试去调用特殊方法</li>
<li>特殊方法将会在特殊的时刻自动调用</li>
<li>学习特殊方法：</li>
</ol>
<ul>
<li>特殊方法什么时候调用</li>
<li>特殊方法有什么作用</li>
</ul>
<ol start="6">
<li><em>_init_\</em>(self): 初始化方法，对象创建后执行，初始化属性和方法<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name</span>):</span></span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">say_hello</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;大家好，我是%s&#x27;</span> % self.name)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">p1 = Person(<span class="string">&#x27;孙悟空&#x27;</span>)</span><br><span class="line">print(p1.say_hello())</span><br></pre></td></tr></table></figure>
<h2 id="封装"><a href="#封装" class="headerlink" title="封装"></a>封装</h2></li>
<li>封装指的是隐藏对象中一些不希望被外部所访问到的属性或方法</li>
<li>隐藏对象属性: 属性前加上<code>__</code>前缀【<font color=red size=3><strong>双下划线</strong></font>】</li>
</ol>
<ul>
<li>隐藏属性只能在类的内部访问，无法通过对象访问</li>
<li>实质是将名字修改为了，_类名__属性名 比如 __name -&gt; _Person__name</li>
<li>因此隐藏属性后可以访问改变后的名字就可以在外部访问</li>
</ul>
<ol start="3">
<li><font color=red size=3><strong>一般我们会将一些私有属性（不希望被外部访问的属性）以_开头【单下划线】</strong></font><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self,name</span>):</span></span><br><span class="line">        self._name = name</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">get_name</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">return</span> self._name</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">set_name</span>(<span class="params">self , name</span>):</span></span><br><span class="line">        self._name = name   </span><br><span class="line"></span><br><span class="line">p = Person(<span class="string">&#x27;孙悟空&#x27;</span>)</span><br><span class="line"></span><br><span class="line">print(p._name) <span class="comment">#孙悟空</span></span><br></pre></td></tr></table></figure></li>
<li>property封装器，用来将一个get方法，转换为对象的属性</li>
</ol>
<ul>
<li>添加为property装饰器以后，我们就可以像调用属性一样使用get方法</li>
<li>使用property装饰的方法，必须和属性名是一样的</li>
</ul>
<ol start="5">
<li>setter方法的装饰器：@属性名.setter<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self,name,age</span>):</span></span><br><span class="line">        self._name = name</span><br><span class="line">        self._age = age</span><br><span class="line"></span><br><span class="line"><span class="meta">    @property</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">name</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;get方法执行了~~~&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span> self._name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @name.setter</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">name</span>(<span class="params">self , name</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;setter方法调用了&#x27;</span>)</span><br><span class="line">        self._name = name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @property</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">age</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">return</span> self._age</span><br><span class="line"></span><br><span class="line"><span class="meta">    @age.setter</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">age</span>(<span class="params">self , age</span>):</span></span><br><span class="line">        self._age = age</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">p = Person(<span class="string">&#x27;猪八戒&#x27;</span>, <span class="number">18</span>)</span><br><span class="line">p.name = <span class="string">&#x27;孙悟空&#x27;</span></span><br><span class="line">p.age = <span class="number">28</span></span><br><span class="line">print(p.name, p.age)</span><br></pre></td></tr></table></figure>
<h2 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h2></li>
<li>通过继承可以直接让子类获取到父类的方法或属性，避免编写重复性的代码，并且也符合OCP原则<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">run</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;动物会跑~~~&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">sleep</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;动物睡觉~~~&#x27;</span>)</span><br><span class="line">        </span><br><span class="line"><span class="comment">#通过用括号引入父类(Animal)，并覆写父类的方法</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span>(<span class="params">Animal</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">bark</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;汪汪汪~~~&#x27;</span>) </span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">run</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;狗跑~~~~&#x27;</span>)    </span><br><span class="line"></span><br><span class="line"><span class="comment">#继承父类(Dog)</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Hashiqi</span>(<span class="params">Dog</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">fan_sha</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;我是一只傻傻的哈士奇&#x27;</span>) </span><br></pre></td></tr></table></figure></li>
<li>在创建类时，如果省略了父类，则默认父类为object</li>
</ol>
<ul>
<li>object是所有类的父类，所有类都继承自object<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line">    <span class="keyword">pass</span></span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="3">
<li>issubclass(): 检查一个类是否是另一个类的子类<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="built_in">issubclass</span>(Animal , Dog)) <span class="comment">#false</span></span><br><span class="line">print(<span class="built_in">issubclass</span>(Animal , <span class="built_in">object</span>)) <span class="comment">#true</span></span><br></pre></td></tr></table></figure></li>
<li>isinstance(): 用来检查一个对象是否是一个类的实例</li>
</ol>
<ul>
<li>如果这个类是这个对象的父类，也会返回True</li>
<li>所有的对象都是object的实例<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="built_in">isinstance</span>(<span class="built_in">print</span> , <span class="built_in">object</span>)) <span class="comment">#true</span></span><br></pre></td></tr></table></figure></li>
</ul>
<ol start="5">
<li>父类中的所有方法都会被子类继承，包括特殊方法，也可以重写特殊方法<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self,name</span>):</span></span><br><span class="line">        self._name = name</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">run</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;动物会跑~~~&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">sleep</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;动物睡觉~~~&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">    @property</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">name</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">return</span> self._name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @name.setter</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">name</span>(<span class="params">self,name</span>):</span></span><br><span class="line">        self._name = name</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span>(<span class="params">Animal</span>):</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self,name,age</span>):</span></span><br><span class="line">        <span class="comment"># 希望可以直接调用父类的__init__来初始化父类中定义的属性</span></span><br><span class="line">        <span class="comment"># super() 可以用来获取当前类的父类，</span></span><br><span class="line">        <span class="comment">#   并且通过super()返回对象调用父类方法时，不需要传递self</span></span><br><span class="line">        <span class="built_in">super</span>().__init__(name)</span><br><span class="line">        self._age = age</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">bark</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;汪汪汪~~~&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">run</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;狗跑~~~~&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">    @property</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">age</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">return</span> self._age</span><br><span class="line"></span><br><span class="line"><span class="meta">    @age.setter</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">age</span>(<span class="params">self,age</span>):</span></span><br><span class="line">        self._age = age</span><br><span class="line"></span><br><span class="line">d = Dog(<span class="string">&#x27;旺财&#x27;</span>,<span class="number">18</span>)</span><br><span class="line"></span><br><span class="line">print(d.name)</span><br><span class="line">print(d.age)</span><br></pre></td></tr></table></figure></li>
<li>多继承: 前边父类的方法会覆盖后边父类的方法</li>
</ol>
<ul>
<li>类名.__bases__ 这个属性可以用来获取当前类的所有父类<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">C</span>(<span class="params">A,B</span>):</span></span><br><span class="line">    <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="多态"><a href="#多态" class="headerlink" title="多态"></a>多态</h2><ul>
<li>多态从字面上理解是多种形态</li>
<li>狗（狼狗、藏獒、哈士奇、古牧 。。。）</li>
<li>一个对象可以以不同的形态去呈现<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个函数</span></span><br><span class="line"><span class="comment"># 对于say_hello()这个函数来说，只要对象中含有name属性，它就可以作为参数传递</span></span><br><span class="line"><span class="comment">#   这个函数并不会考虑对象的类型，只要有name属性即可</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">say_hello</span>(<span class="params">obj</span>):</span></span><br><span class="line">    print(<span class="string">&#x27;你好 %s&#x27;</span>%obj.name)</span><br></pre></td></tr></table></figure>
<h2 id="类中的属性和方法"><a href="#类中的属性和方法" class="headerlink" title="类中的属性和方法"></a>类中的属性和方法</h2></li>
</ul>
<ol>
<li>类属性: 直接在类中定义的属性是类属性</li>
</ol>
<ul>
<li>类属性可以通过类或类的实例访问到</li>
<li>但是类属性只能通过类对象【<font color=red size=3><strong>即A.xxx，A是一个类</strong></font>】来修改，无法通过实例对象修改</li>
</ul>
<ol start="2">
<li>实例属性： 通过实例对象添加的属性属于实例属性</li>
</ol>
<ul>
<li>实例属性只能通过实例对象来访问和修改，类对象无法访问修改</li>
</ul>
<ol start="3">
<li>实例方法：在类中定义，以self为第一个参数的方法都是实例方法</li>
</ol>
<ul>
<li>实例方法在调用时，Python会将调用对象作为self传入 </li>
<li>实例方法可以通过实例和类去调用<ul>
<li>当通过实例调用时，会自动将当前调用对象作为self传入</li>
<li>当通过类调用时，不会自动传递self，此时我们必须手动传递self<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 类属性</span></span><br><span class="line">    <span class="comment"># 实例属性</span></span><br><span class="line">    <span class="comment"># 类方法</span></span><br><span class="line">    <span class="comment"># 实例方法</span></span><br><span class="line">    <span class="comment"># 静态方法</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 类属性，直接在类中定义的属性是类属性</span></span><br><span class="line">    <span class="comment">#   类属性可以通过类或类的实例访问到</span></span><br><span class="line">    <span class="comment">#   但是类属性只能通过类对象来修改，无法通过实例对象修改</span></span><br><span class="line">    count = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="comment"># 实例属性，通过实例对象添加的属性属于实例属性</span></span><br><span class="line">        <span class="comment">#   实例属性只能通过实例对象来访问和修改，类对象无法访问修改</span></span><br><span class="line">        self.name = <span class="string">&#x27;孙悟空&#x27;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 实例方法</span></span><br><span class="line">    <span class="comment">#   在类中定义，以self为第一个参数的方法都是实例方法</span></span><br><span class="line">    <span class="comment">#   实例方法在调用时，Python会将调用对象作为self传入  </span></span><br><span class="line">    <span class="comment">#   实例方法可以通过实例和类去调用</span></span><br><span class="line">    <span class="comment">#       当通过实例调用时，会自动将当前调用对象作为self传入</span></span><br><span class="line">    <span class="comment">#       当通过类调用时，不会自动传递self，此时我们必须手动传递self</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">test</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;这是test方法~~~ &#x27;</span> , self)    </span><br><span class="line"></span><br><span class="line">    <span class="comment"># 类方法    </span></span><br><span class="line">    <span class="comment"># 在类内部使用 @classmethod 来修饰的方法属于类方法</span></span><br><span class="line">    <span class="comment"># 类方法的第一个参数是cls，也会被自动传递，cls就是当前的类对象</span></span><br><span class="line">    <span class="comment">#   类方法和实例方法的区别，实例方法的第一个参数是self，而类方法的第一个参数是cls</span></span><br><span class="line">    <span class="comment">#   类方法可以通过类去调用，也可以通过实例调用，没有区别</span></span><br><span class="line"><span class="meta">    @classmethod</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">test_2</span>(<span class="params">cls</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;这是test_2方法，他是一个类方法~~~ &#x27;</span>,cls)</span><br><span class="line">        print(cls.count)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 静态方法</span></span><br><span class="line">    <span class="comment"># 在类中使用 @staticmethod 来修饰的方法属于静态方法  </span></span><br><span class="line">    <span class="comment"># 静态方法不需要指定任何的默认参数，静态方法可以通过类和实例去调用  </span></span><br><span class="line">    <span class="comment"># 静态方法，基本上是一个和当前类无关的方法，它只是一个保存到当前类中的函数</span></span><br><span class="line">    <span class="comment"># 静态方法一般都是一些工具方法，和当前类无关</span></span><br><span class="line"><span class="meta">    @staticmethod</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">test_3</span>():</span></span><br><span class="line">        print(<span class="string">&#x27;test_3执行了~~~&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">a = A()</span><br><span class="line"><span class="comment"># 实例属性，通过实例对象添加的属性属于实例属性</span></span><br><span class="line">a.count = <span class="number">10</span></span><br><span class="line">A.count = <span class="number">100</span></span><br><span class="line">print(<span class="string">&#x27;A ,&#x27;</span>,A.count) </span><br><span class="line">print(<span class="string">&#x27;a ,&#x27;</span>,a.count) </span><br><span class="line">print(<span class="string">&#x27;A ,&#x27;</span>,A.name) <span class="comment">#会报错，没有在该类属性</span></span><br><span class="line">print(<span class="string">&#x27;a ,&#x27;</span>,a.name)   </span><br><span class="line"></span><br><span class="line">a.test() <span class="comment">#等价于 A.test(a)</span></span><br><span class="line"></span><br><span class="line">A.test_2() <span class="comment">#等价于 a.test_2()</span></span><br><span class="line"></span><br><span class="line">A.test_3()</span><br><span class="line">a.test_3()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="垃圾回收"><a href="#垃圾回收" class="headerlink" title="垃圾回收"></a>垃圾回收</h2></li>
</ul>
</li>
<li>就像我们生活中会产生垃圾一样，程序在运行过程当中也会产生垃圾</li>
<li>程序运行过程中产生的垃圾会影响到程序的运行的运行性能，所以这些垃圾必须被及时清理</li>
<li>没用的东西就是垃圾</li>
<li>在程序中没有被引用的对象就是垃圾，这种垃圾对象过多以后会影响到程序的运行的性能<ul>
<li>所以我们必须进行及时的垃圾回收，所谓的垃圾回收就是讲垃圾对象从内存中删除</li>
</ul>
</li>
<li>在Python中有自动的垃圾回收机制，它会自动将这些没有被引用的对象删除，<ul>
<li><p>所以我们不用手动处理垃圾回收</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">        self.name = <span class="string">&#x27;A类&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># del是一个特殊方法，它会在对象被垃圾回收前调用</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__del__</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&#x27;A()对象被删除了~~~&#x27;</span>,self)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = A()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="literal">None</span> <span class="comment"># 将a设置为了None，此时没有任何的变量对A()对象进行引用，它就是变成了垃圾</span></span><br><span class="line">A()对象被删除了~~~ &lt;__main__.A <span class="built_in">object</span> at <span class="number">0x000001103EC265F8</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 又使用一个变量b，来引用a对应的对象,这时A()对象由b进行引用，不是垃圾</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = A()</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>b = a</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="literal">None</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(a.name)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">del</span> b  <span class="comment">#手动删除变量b的引用</span></span><br><span class="line">A()对象被删除了~~~ &lt;__main__.A <span class="built_in">object</span> at <span class="number">0x000001103EC265F8</span>&gt;</span><br></pre></td></tr></table></figure>
<h2 id="特殊方法-1"><a href="#特殊方法-1" class="headerlink" title="特殊方法"></a>特殊方法</h2></li>
</ul>
</li>
</ul>
<ol>
<li>特殊方法，也被成为魔法方法</li>
<li>特殊方法都是使用__开头和结尾的</li>
<li>特殊方法一般不需要我们手动调用，需要在一些特殊情况下自动执行</li>
</ol>
<h3 id="str"><a href="#str" class="headerlink" title="__str__()"></a>__str__()</h3><ul>
<li>当打印一个对象时，实际上打印的是对象的中特殊方法 __str__()的返回值</li>
<li> __str__（）这个特殊方法会在尝试将对象转换为字符串的时候调用</li>
<li>它的作用可以用来指定对象转换为字符串的结果  （print函数）<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个Person类4</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="class"><span class="keyword">class</span> <span class="title">Person</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span>    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name , age</span>):</span></span><br><span class="line"><span class="meta">... </span>            self.name = name</span><br><span class="line"><span class="meta">... </span>            self.age = age</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>p1 = Person(<span class="string">&#x27;孙悟空&#x27;</span>,<span class="number">18</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(p1) <span class="comment">#当打印一个对象时，实际上打印的是对象的中特殊方法 __str__()的返回值</span></span><br><span class="line">&lt;__main__.Person <span class="built_in">object</span> at <span class="number">0x000001D1CAAFA160</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">#自定义__str__()方法</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="class"><span class="keyword">class</span> <span class="title">Person</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span>    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name , age</span>):</span></span><br><span class="line"><span class="meta">... </span>            self.name = name</span><br><span class="line"><span class="meta">... </span>            self.age = age</span><br><span class="line"><span class="meta">... </span>    <span class="function"><span class="keyword">def</span> <span class="title">__str__</span>(<span class="params">self</span>):</span></span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> <span class="string">&#x27;Person [name=%s , age=%d]&#x27;</span>%(self.name,self.age)</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>p1 = Person(<span class="string">&#x27;孙悟空&#x27;</span>,<span class="number">18</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(p1)</span><br><span class="line">Person [name=孙悟空 , age=<span class="number">18</span>]</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="repr"><a href="#repr" class="headerlink" title="_\repr__()"></a>_\repr__()</h3></li>
<li>这个特殊方法会在对当前对象使用repr()函数时调用</li>
<li>它的作用是指定对象在 ‘交互模式’中直接输出的效果<ul>
<li>比如一个变量a = ‘hello’,如果是print(a)，则调用的是str这个特殊方法，打印hello；<br>而如果直接输出a，则调用repr这个特殊方法，打印’hello’<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在cmd命令行的交互模式下才起作用</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a = <span class="string">&#x27;hello&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(a)</span><br><span class="line">hello</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>a</span><br><span class="line"><span class="string">&#x27;hello&#x27;</span></span><br></pre></td></tr></table></figure>
<h3 id="其他特殊方法"><a href="#其他特殊方法" class="headerlink" title="其他特殊方法"></a>其他特殊方法</h3></li>
</ul>
</li>
</ul>
<ol>
<li>object.__lt__(self, other) 小于 &lt;</li>
<li>object.__le__(self, other) 小于等于 &lt;=</li>
<li>object.__eq__(self, other) 等于 ==</li>
<li>object.__ne__(self, other) 不等于 !=</li>
<li>object.__gt__(self, other) 大于 &gt;<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="class"><span class="keyword">class</span> <span class="title">Person</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line"><span class="meta">... </span>    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name , age</span>):</span></span><br><span class="line"><span class="meta">... </span>            self.name = name</span><br><span class="line"><span class="meta">... </span>            self.age = age</span><br><span class="line"><span class="meta">... </span>    <span class="function"><span class="keyword">def</span> <span class="title">__gt__</span>(<span class="params">self , other</span>):</span></span><br><span class="line"><span class="meta">... </span>            <span class="keyword">return</span> self.age &gt; other.age</span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>p1 = Person(<span class="string">&#x27;孙悟空&#x27;</span>,<span class="number">18</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>p2 = Person(<span class="string">&#x27;猪八戒&#x27;</span>,<span class="number">28</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(p1 &gt; p2)  <span class="comment">#直接使用大于号作比较，其他类似</span></span><br><span class="line"><span class="literal">False</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(p2 &gt; p1)</span><br><span class="line"><span class="literal">True</span></span><br></pre></td></tr></table></figure></li>
<li>object.__ge__(self, other) 大于等于 &gt;= </li>
</ol>
<h2 id="模块（module）"><a href="#模块（module）" class="headerlink" title="模块（module）"></a>模块（module）</h2><ol>
<li>模块化，模块化指将一个完整的程序分解为一个一个小的模块</li>
</ol>
<ul>
<li>通过将模块组合，来搭建出一个完整的程序</li>
</ul>
<ol start="2">
<li>不采用模块化，统一将所有的代码编写到一个文件中</li>
<li>采用模块化，将程序分别编写到多个文件中</li>
<li>模块化的有点：</li>
</ol>
<ul>
<li>方便开发</li>
<li>方便维护</li>
<li>模块可以复用！</li>
</ul>
<ol start="5">
<li>在Python中一个py文件就是一个模块，要想创建模块，实际上就是创建一个python文件</li>
</ol>
<ul>
<li>注意：模块名要符号标识符的规范</li>
</ul>
<ol start="6">
<li>在一个模块中引入外部模块</li>
</ol>
<ul>
<li>import 模块名 （模块名，就是python文件的名字，注意不要py）</li>
<li>import 模块名 as 模块别名<ul>
<li>可以引入同一个模块多次，但是模块的实例只会创建一个</li>
<li>import可以在程序的任意位置调用，但是一般情况下，import语句都会统一写在程序的开头</li>
<li>在每一个模块内部都有一个__name__属性，通过这个属性可以获取到模块的名字</li>
<li>__name__属性值为 __main__的模块是主模块，一个程序中只会有一个主模块</li>
<li><font color=red size=3><strong>主模块就是我们直接通过 python 执行的模块</strong></font></li>
</ul>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> test_module <span class="keyword">as</span> test</span><br><span class="line"></span><br><span class="line">print(test.__name__)</span><br><span class="line">print(__name__) <span class="comment">#当前执行的主程序</span></span><br></pre></td></tr></table></figure>
<ol start="7">
<li>可以为引入的变量使用别名</li>
</ol>
<ul>
<li>语法：from 模块名 import 变量 as 别名</li>
<li>from m import test2 as new_test2</li>
</ul>
<h2 id="包（package）"><a href="#包（package）" class="headerlink" title="包（package）"></a>包（package）</h2><ul>
<li>包也是一个模块</li>
<li>当我们模块中代码过多时，或者一个模块需要被分解为多个模块时，这时就需要使用到包</li>
<li>普通的模块就是一个py文件，而包是一个文件夹</li>
<li>包中必须要一个 __init__.py 这个文件，这个文件中可以包含有包中的主要内容</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># hello是一个包，也是一个文件夹，包含有__init__.py,a.py,b.py这三个文件，指定引入hello包下的</span></span><br><span class="line">a模块和b模块</span><br><span class="line"><span class="keyword">from</span> hello <span class="keyword">import</span> a , b</span><br><span class="line"></span><br><span class="line">print(a.c)</span><br><span class="line">print(b.d)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li>__pycache__ 是模块的缓存文件</li>
<li>py代码在执行前，需要被解析器先转换为机器码，然后再执行</li>
<li>所以我们在使用模块（包）时，也需要将模块的代码先转换为机器码然后再交由计算机执行</li>
<li>而为了提高程序运行的性能，python会在编译过一次以后，将代码保存到一个缓存文件中</li>
<li>这样在下次加载这个模块（包）时，就可以不再重新编译而是直接加载缓存中编译好的代码即可</li>
</ul>
<h1 id="第九章-异常和文件"><a href="#第九章-异常和文件" class="headerlink" title="第九章 异常和文件"></a>第九章 异常和文件</h1><h2 id="异常"><a href="#异常" class="headerlink" title="异常"></a>异常</h2><h3 id="异常说明"><a href="#异常说明" class="headerlink" title="异常说明"></a>异常说明</h3><ol>
<li>程序在运行过程当中，不可避免的会出现一些错误，比如：</li>
</ol>
<ul>
<li>使用了没有赋值过的变量</li>
<li>使用了不存在的索引</li>
<li>除0</li>
<li>…</li>
</ul>
<ol start="2">
<li>这些错误在程序中，我们称其为异常。</li>
<li>程序运行过程中，一旦出现异常将会导致程序立即终止，异常以后的代码全部都不会执行！    </li>
</ol>
<h3 id="处理异常"><a href="#处理异常" class="headerlink" title="处理异常"></a>处理异常</h3><ul>
<li>程序运行时出现异常，目的并不是让我们的程序直接终止！</li>
<li>Python是希望在出现异常时，我们可以编写代码来对异常进行处理！    </li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>语句</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        代码块（可能出现错误的语句）</span><br><span class="line">    <span class="keyword">except</span> 异常类型 <span class="keyword">as</span> 异常名:</span><br><span class="line">        代码块（出现错误以后的处理方式）</span><br><span class="line">    <span class="keyword">except</span> 异常类型 <span class="keyword">as</span> 异常名:</span><br><span class="line">        代码块（出现错误以后的处理方式）</span><br><span class="line">    <span class="keyword">except</span> 异常类型 <span class="keyword">as</span> 异常名:</span><br><span class="line">        代码块（出现错误以后的处理方式）</span><br><span class="line">    <span class="keyword">else</span>：</span><br><span class="line">        代码块（没出错时要执行的语句）    </span><br><span class="line">    <span class="keyword">finally</span>:</span><br><span class="line">        代码块（该代码块总会执行）    </span><br></pre></td></tr></table></figure>
<ul>
<li><p>try是必须的 else语句有没有都行</p>
</li>
<li><p>except和finally至少有一个    </p>
</li>
<li><p>可以将可能出错的代码放入到try语句，这样如果代码没有错误，则会正常执行，</p>
<ul>
<li>如果出现错误，则会执行expect子句中的代码，这样我们就可以通过代码来处理异常</li>
<li>避免因为一个异常导致整个程序的终止            </li>
</ul>
</li>
</ul>
<h3 id="异常的传播（抛出异常）"><a href="#异常的传播（抛出异常）" class="headerlink" title="异常的传播（抛出异常）"></a>异常的传播（抛出异常）</h3><ul>
<li><p>当在函数中出现异常时，如果在函数中对异常进行了处理，则异常不会再继续传播,</p>
<ul>
<li>如果函数中没有对异常进行处理，则异常会继续向函数调用处传播,</li>
<li>如果函数调用处处理了异常，则不再传播，如果没有处理则继续向调用处传播</li>
<li>直到传递到全局作用域（主模块）如果依然没有处理，则程序终止，并且显示异常信息</li>
</ul>
</li>
<li><p>当程序运行过程中出现异常以后，所有的异常信息会被保存一个专门的异常对象中，</p>
<ul>
<li>而异常传播时，实际上就是异常对象抛给了调用处</li>
<li>比如 ： ZeroDivisionError类的对象专门用来表示除0的异常<ul>
<li>NameError类的对象专门用来处理变量错误的异常</li>
<li>….</li>
</ul>
</li>
</ul>
</li>
<li><p>在Python为我们提供了多个异常对象            </p>
</li>
</ul>
<h3 id="抛出异常"><a href="#抛出异常" class="headerlink" title="抛出异常"></a>抛出异常</h3><ul>
<li>可以使用 raise 语句来抛出异常，<ul>
<li>raise语句后需要跟一个异常类 或 异常的实例<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 也可以自定义异常类，只需要创建一个类继承Exception即可</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyError</span>(<span class="params">Exception</span>):</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">add</span>(<span class="params">a,b</span>):</span></span><br><span class="line">    <span class="comment"># 如果a和b中有负数，就向调用处抛出异常</span></span><br><span class="line">    <span class="keyword">if</span> a &lt; <span class="number">0</span> <span class="keyword">or</span> b &lt; <span class="number">0</span>:</span><br><span class="line">        <span class="comment"># raise用于向外部抛出异常，后边可以跟一个异常类，或异常类的实例</span></span><br><span class="line">        <span class="comment"># raise Exception    </span></span><br><span class="line">        <span class="comment"># 抛出异常的目的，告诉调用者这里调用时出现问题，希望你自己处理一下</span></span><br><span class="line">        <span class="comment"># raise Exception(&#x27;两个参数中不能有负数！&#x27;)  </span></span><br><span class="line">        <span class="keyword">raise</span> MyError(<span class="string">&#x27;自定义的异常&#x27;</span>)</span><br><span class="line">        </span><br><span class="line">        <span class="comment"># 也可以通过if else来代替异常的处理</span></span><br><span class="line">        <span class="comment"># return None</span></span><br><span class="line">    r = a + b</span><br><span class="line">    <span class="keyword">return</span> r</span><br><span class="line"></span><br><span class="line">print(add(-<span class="number">123</span>,<span class="number">456</span>))</span><br></pre></td></tr></table></figure>
<h2 id="文件（File）"><a href="#文件（File）" class="headerlink" title="文件（File）"></a>文件（File）</h2></li>
</ul>
</li>
<li>通过Python程序来对计算机中的各种文件进行增删改查的操作</li>
<li>I/O(Input / Output)</li>
<li>操作文件的步骤：</li>
</ul>
<ol>
<li>打开文件</li>
<li>对文件进行各种操作（读、写），然后保存</li>
<li>关闭文件</li>
</ol>
<h3 id="打开文件（open函数）"><a href="#打开文件（open函数）" class="headerlink" title="打开文件（open函数）"></a>打开文件（open函数）</h3><ul>
<li><p>open(file, mode=’r’, buffering=-1, encoding_=None, errors=None, newline=None, closefd=True, opener=None)</p>
</li>
<li><p>使用open函数来打开一个文件</p>
</li>
<li><p>参数：</p>
<ul>
<li>file 要打开的文件的名字（路径）</li>
</ul>
</li>
<li><p>返回值：</p>
<ul>
<li>返回一个对象，这个对象就代表了当前打开的文件</li>
</ul>
</li>
<li><p>创建一个变量，来保存文件的名字</p>
</li>
<li><p>如果目标文件和当前文件在同一级目录下，则直接使用文件名即可</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">file_name = <span class="string">&#x27;demo.txt&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 在windows系统使用路径时，可以使用/来代替 \</span></span><br><span class="line"><span class="comment"># 或者可以使用 \\ 来代替 \</span></span><br><span class="line"><span class="comment"># 或者也可以使用原始字符串</span></span><br><span class="line">file_name = <span class="string">&#x27;hello\\demo.txt&#x27;</span></span><br><span class="line">file_name = <span class="string">r&#x27;hello\demo.txt&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 表示路径，可以使用..来返回一级目录</span></span><br><span class="line">file_name = <span class="string">&#x27;../hello/demo.txt&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果目标文件距离当前文件比较远，此时可以使用绝对路径</span></span><br><span class="line"><span class="comment"># 绝对路径应该从磁盘的根目录开始书写</span></span><br><span class="line">file_name = <span class="string">r&#x27;C:\Users\lilichao\Desktop\hello.txt&#x27;</span></span><br><span class="line"></span><br><span class="line">file_obj = <span class="built_in">open</span>(file_name) <span class="comment"># 打开 file_name 对应的文件</span></span><br><span class="line"></span><br><span class="line">print(file_obj)</span><br></pre></td></tr></table></figure>
<h3 id="关闭文件（close函数）"><a href="#关闭文件（close函数）" class="headerlink" title="关闭文件（close函数）"></a>关闭文件（close函数）</h3></li>
<li><p>当我们获取了文件对象以后，所有的对文件的操作都应该通过对象来进行</p>
</li>
<li><p>调用close()方法来关闭文件</p>
</li>
<li><p>关闭文件可以提升程序性能，如果有多个进程读取同一个文件不关闭就会在内存中占有多个资源而不被释放</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 打开文件</span></span><br><span class="line">file_name = <span class="string">&#x27;demo.txt&#x27;</span></span><br><span class="line">file_obj = <span class="built_in">open</span>(file_name)</span><br><span class="line"><span class="comment"># 关闭文件</span></span><br><span class="line"><span class="comment"># 调用close()方法来关闭文件</span></span><br><span class="line">file_obj.close()</span><br></pre></td></tr></table></figure></li>
<li><p>with … as 语句</p>
<ul>
<li>在with语句中可以直接使用file_obj来做文件操作</li>
<li>此时这个文件只能在with中使用，一旦with结束则文件会自动close()<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">#with ... as 语句</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(file_name) <span class="keyword">as</span> file_obj :</span><br><span class="line">    <span class="comment"># 在with语句中可以直接使用file_obj来做文件操作</span></span><br><span class="line">    <span class="comment"># 此时这个文件只能在with中使用，一旦with结束则文件会自动close()</span></span><br><span class="line">    print(file_obj.read())</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>try捕获文件找不到问题</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">file_name = <span class="string">&#x27;hello&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(file_name) <span class="keyword">as</span> file_obj :</span><br><span class="line">        print(file_obj.read())</span><br><span class="line"><span class="keyword">except</span> FileNotFoundError:</span><br><span class="line">    print(<span class="string">f&#x27;<span class="subst">&#123;file_name&#125;</span> 文件不存在~~&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="文件读取"><a href="#文件读取" class="headerlink" title="文件读取"></a>文件读取</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">file_name = <span class="string">&#x27;demo2.txt&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="comment"># 调用open()来打开一个文件，可以将文件分成两种类型</span></span><br><span class="line">    <span class="comment"># 一种，是纯文本文件（使用utf-8等编码编写的文本文件）</span></span><br><span class="line">    <span class="comment"># 一种，是二进制文件（图片、mp3、ppt等这些文件）</span></span><br><span class="line">    <span class="comment"># open()打开文件时，默认是以文本文件的形式打开的，但是open()默认的编码为None</span></span><br><span class="line">    <span class="comment">#   所以处理文本文件时，必须要指定文件的编码</span></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(file_name,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> file_obj:</span><br><span class="line">        <span class="comment"># 通过 read() 来读取文件中的内容</span></span><br><span class="line">        <span class="comment"># 如果直接调用read()它会将文本文件的所有内容全部都读取出来</span></span><br><span class="line">        <span class="comment">#   如果要读取的文件较大的话，会一次性将文件的内容加载到内存中，容易导致内存泄漏</span></span><br><span class="line">        <span class="comment">#   所以对于较大的文件，不要直接调用read()</span></span><br><span class="line">        </span><br><span class="line">        <span class="built_in">help</span>(file_obj.read) 帮助信息</span><br><span class="line">        </span><br><span class="line">        <span class="comment"># read()可以接收一个size作为参数，该参数用来指定要读取的字符的数量</span></span><br><span class="line">        <span class="comment">#   默认值为-1，它会读取文件中的所有字符</span></span><br><span class="line">        <span class="comment">#   可以为size指定一个值，这样read()会读取指定数量的字符，</span></span><br><span class="line">        <span class="comment">#       每一次读取都是从上次读取到位置开始读取的</span></span><br><span class="line">        <span class="comment">#       如果字符的数量小于size，则会读取剩余所有的</span></span><br><span class="line">        <span class="comment">#       如果已经读取到了文件的最后了，则会返回&#x27;&#x27;空串</span></span><br><span class="line">        <span class="comment"># content = file_obj.read(-1)</span></span><br><span class="line">        </span><br><span class="line">        content = file_obj.read(<span class="number">6</span>)</span><br><span class="line">        content = file_obj.read(<span class="number">6</span>)</span><br><span class="line">        content = file_obj.read(<span class="number">6</span>)</span><br><span class="line">        content = file_obj.read(<span class="number">6</span>)</span><br><span class="line">        <span class="comment"># print(content)</span></span><br><span class="line">        <span class="comment"># print(len(content))</span></span><br><span class="line"><span class="keyword">except</span> FileNotFoundError :</span><br><span class="line">    print(<span class="string">f&#x27;<span class="subst">&#123;file_name&#125;</span> 这个文件不存在！&#x27;</span>)</span><br></pre></td></tr></table></figure></li>
<li><p>读取大文件的方式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">file_name = <span class="string">&#x27;demo.txt&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(file_name,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> file_obj:</span><br><span class="line">        <span class="comment"># 定义一个变量，来保存文件的内容</span></span><br><span class="line">        file_content = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="comment"># 定义一个变量，来指定每次读取的大小</span></span><br><span class="line">        chunk = <span class="number">100</span></span><br><span class="line">        <span class="comment"># 创建一个循环来读取文件内容</span></span><br><span class="line">        <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">            <span class="comment"># 读取chunk大小的内容</span></span><br><span class="line">            content = file_obj.read(chunk)</span><br><span class="line"></span><br><span class="line">            <span class="comment"># 检查是否读取到了内容</span></span><br><span class="line">            <span class="keyword">if</span> <span class="keyword">not</span> content:</span><br><span class="line">                <span class="comment"># 内容读取完毕，退出循环</span></span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">            <span class="comment"># 输出内容</span></span><br><span class="line">            <span class="comment"># print(content,end=&#x27;&#x27;)</span></span><br><span class="line">            file_content += content</span><br><span class="line"></span><br><span class="line"><span class="keyword">except</span> FileNotFoundError :</span><br><span class="line">    print(<span class="string">f&#x27;<span class="subst">&#123;file_name&#125;</span> 这个文件不存在！&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">print(file_content)</span><br></pre></td></tr></table></figure></li>
<li><p>readline()： 该方法可以用来读取一行内容</p>
</li>
<li><p>readlines()：该方法用于一行一行的读取内容，它会一次性将读取到的内容封装到一个列表中返回</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pprint</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line">file_name = <span class="string">&#x27;demo.txt&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(file_name , encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> file_obj:</span><br><span class="line">    <span class="comment"># readline()</span></span><br><span class="line">    <span class="comment"># 该方法可以用来读取一行内容</span></span><br><span class="line">    print(file_obj.readline(),end=<span class="string">&#x27;&#x27;</span>)</span><br><span class="line">    print(file_obj.readline())</span><br><span class="line">    print(file_obj.readline())</span><br><span class="line"></span><br><span class="line">    <span class="comment"># readlines()</span></span><br><span class="line">    <span class="comment"># 该方法用于一行一行的读取内容，它会一次性将读取到的内容封装到一个列表中返回</span></span><br><span class="line">    r = file_obj.readlines()</span><br><span class="line">    pprint.pprint(r[<span class="number">0</span>])</span><br><span class="line">    pprint.pprint(r[<span class="number">1</span>])</span><br><span class="line">    pprint.pprint(r[<span class="number">2</span>])</span><br><span class="line"></span><br><span class="line">    <span class="comment">#for循环也可以循环每一行读取文件内容</span></span><br><span class="line">    <span class="keyword">for</span> t <span class="keyword">in</span> file_obj:</span><br><span class="line">        print(t)</span><br></pre></td></tr></table></figure>
<h4 id="读取二进制文件"><a href="#读取二进制文件" class="headerlink" title="读取二进制文件"></a>读取二进制文件</h4></li>
<li><p>读取模式</p>
<ul>
<li>t 读取文本文件（默认值）</li>
<li>b 读取二进制文件</li>
</ul>
</li>
<li><p>读取文本文件时，size是以字符为单位的</p>
</li>
<li><p>读取二进制文件时，size是以字节为单位</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">file_name = <span class="string">&#x27;c:/Users/lilichao/Desktop/告白气球.flac&#x27;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(file_name , <span class="string">&#x27;rb&#x27;</span>) <span class="keyword">as</span> file_obj:</span><br><span class="line">    <span class="comment"># print(file_obj.read(100))</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 将读取到的内容写出来</span></span><br><span class="line">    <span class="comment"># 定义一个新的文件</span></span><br><span class="line">    new_name = <span class="string">&#x27;aa.flac&#x27;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(new_name , <span class="string">&#x27;wb&#x27;</span>) <span class="keyword">as</span> new_obj:</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 定义每次读取的大小</span></span><br><span class="line">        chunk = <span class="number">1024</span> * <span class="number">100</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> <span class="literal">True</span> :</span><br><span class="line">            <span class="comment"># 从已有的对象中读取数据</span></span><br><span class="line">            content = file_obj.read(chunk)</span><br><span class="line"></span><br><span class="line">            <span class="comment"># 内容读取完毕，终止循环</span></span><br><span class="line">            <span class="keyword">if</span> <span class="keyword">not</span> content :</span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">            <span class="comment"># 将读取到的数据写入到新对象中</span></span><br><span class="line">            new_obj.write(content)</span><br></pre></td></tr></table></figure>
<h4 id="读取文件的位置（seek函数）"><a href="#读取文件的位置（seek函数）" class="headerlink" title="读取文件的位置（seek函数）"></a>读取文件的位置（seek函数）</h4></li>
<li><p>seek()需要两个参数</p>
<ul>
<li>第一个 是要切换到的位置</li>
<li>第二个 计算位置方式<ul>
<li>可选值：<ol>
<li>从头计算，默认值</li>
<li>从当前位置计算</li>
<li>从最后位置开始计算</li>
</ol>
</li>
</ul>
</li>
</ul>
</li>
<li><p>tell() 方法用来查看当前读取的位置</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;demo2.txt&#x27;</span>,<span class="string">&#x27;rt&#x27;</span> , encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> file_obj:</span><br><span class="line">    <span class="comment"># print(file_obj.read(100))</span></span><br><span class="line">    <span class="comment"># print(file_obj.read(30))</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># seek() 可以修改当前读取的位置</span></span><br><span class="line">    file_obj.seek(<span class="number">9</span>)</span><br><span class="line">    <span class="comment"># seek()需要两个参数</span></span><br><span class="line">    <span class="comment">#   第一个 是要切换到的位置</span></span><br><span class="line">    <span class="comment">#   第二个 计算位置方式</span></span><br><span class="line">    <span class="comment">#       可选值：</span></span><br><span class="line">    <span class="comment">#           0 从头计算，默认值</span></span><br><span class="line">    <span class="comment">#           1 从当前位置计算,比如当前读到100，往后再加9个</span></span><br><span class="line">    <span class="comment">#           2 从最后位置开始计算</span></span><br><span class="line"></span><br><span class="line">    print(file_obj.read())</span><br><span class="line"></span><br><span class="line">    <span class="comment"># tell() 方法用来查看当前读取的位置</span></span><br><span class="line">    print(<span class="string">&#x27;当前读取到了 --&gt;&#x27;</span>,file_obj.tell())</span><br></pre></td></tr></table></figure>
<h3 id="文件写入（write函数）"><a href="#文件写入（write函数）" class="headerlink" title="文件写入（write函数）"></a>文件写入（write函数）</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">file_name = <span class="string">&#x27;demo5.txt&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用open()打开文件时必须要指定打开文件所要做的操作（读、写、追加）</span></span><br><span class="line"><span class="comment"># 如果不指定操作类型，则默认是 读取文件 ， 而读取文件时是不能向文件中写入的</span></span><br><span class="line"><span class="comment"># r 表示只读的</span></span><br><span class="line"><span class="comment"># w 表示是可写的，使用w来写入文件时，如果文件不存在会创建文件，如果文件存在则会截断文件</span></span><br><span class="line"><span class="comment">#   截断文件指删除原来文件中的所有内容</span></span><br><span class="line"><span class="comment"># a 表示追加内容，如果文件不存在会创建文件，如果文件存在则会向文件中追加内容</span></span><br><span class="line"><span class="comment"># x 用来新建文件，如果文件不存在则创建，存在则报错</span></span><br><span class="line"><span class="comment"># + 为操作符增加功能</span></span><br><span class="line"><span class="comment">#   r+ 即可读又可写，文件不存在会报错</span></span><br><span class="line"><span class="comment">#   w+</span></span><br><span class="line"><span class="comment">#   a+</span></span><br><span class="line"><span class="comment"># with open(file_name , &#x27;w&#x27; , encoding=&#x27;utf-8&#x27;) as file_obj:</span></span><br><span class="line"><span class="comment"># with open(file_name , &#x27;r+&#x27; , encoding=&#x27;utf-8&#x27;) as file_obj:</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(file_name , <span class="string">&#x27;x&#x27;</span> , encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> file_obj:</span><br><span class="line">    <span class="comment"># write()来向文件中写入内容，</span></span><br><span class="line">    <span class="comment"># 如果操作的是一个文本文件的话，则write()需要传递一个字符串作为参数</span></span><br><span class="line">    <span class="comment"># 该方法会可以分多次向文件中写入内容</span></span><br><span class="line">    <span class="comment"># 写入完成以后，该方法会返回写入的字符的个数</span></span><br><span class="line">    file_obj.write(<span class="string">&#x27;aaa\n&#x27;</span>)</span><br><span class="line">    file_obj.write(<span class="string">&#x27;bbb\n&#x27;</span>)</span><br><span class="line">    file_obj.write(<span class="string">&#x27;ccc\n&#x27;</span>)</span><br><span class="line">    r = file_obj.write(<span class="built_in">str</span>(<span class="number">123</span>)+<span class="string">&#x27;123123\n&#x27;</span>)</span><br><span class="line">    r = file_obj.write(<span class="string">&#x27;今天天气真不错&#x27;</span>)</span><br><span class="line">    print(r)    <span class="comment">#返回写入的字符数</span></span><br></pre></td></tr></table></figure>
<h3 id="文件的其他操作"><a href="#文件的其他操作" class="headerlink" title="文件的其他操作"></a>文件的其他操作</h3></li>
</ul>
<ol>
<li>os.listdir()： 获取指定目录的目录结构<ul>
<li>需要一个路径作为参数，会获取到该路径下的目录结构，默认路径为<font color=black size=4><strong>.</strong></font>当前目录</li>
<li>该方法会返回一个列表，目录中的每一个文件（夹）的名字都是列表中的一个元素</li>
</ul>
</li>
<li>os.getcwd(): 获取当前所在的目录</li>
<li>os.chdir(): 切换当前所在的目录 作用相当于 cd</li>
<li>os.mkdir(“aaa”): 在当前目录下创建一个名字为 aaa 的目录</li>
<li>os.rmdir(‘abc’): 删除名为abc的目录</li>
<li>os.remove(‘aa.txt’): 删除名为aa.txt的文件</li>
<li>os.rename(‘旧名字’,’新名字’): 可以对一个文件进行重命名，也可以用来移动一个文件<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> pprint <span class="keyword">import</span> pprint</span><br><span class="line"></span><br><span class="line"><span class="comment"># os.listdir() 获取指定目录的目录结构</span></span><br><span class="line"><span class="comment"># 需要一个路径作为参数，会获取到该路径下的目录结构，默认路径为 . 当前目录</span></span><br><span class="line"><span class="comment"># 该方法会返回一个列表，目录中的每一个文件（夹）的名字都是列表中的一个元素</span></span><br><span class="line">r = os.listdir()</span><br><span class="line"></span><br><span class="line"><span class="comment"># os.getcwd() 获取当前所在的目录</span></span><br><span class="line">r = os.getcwd()</span><br><span class="line"></span><br><span class="line"><span class="comment"># os.chdir() 切换当前所在的目录 作用相当于 cd</span></span><br><span class="line"><span class="comment"># os.chdir(&#x27;c:/&#x27;)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># r = os.getcwd()</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建目录</span></span><br><span class="line"><span class="comment"># os.mkdir(&quot;aaa&quot;) # 在当前目录下创建一个名字为 aaa 的目录</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除目录</span></span><br><span class="line"><span class="comment"># os.rmdir(&#x27;abc&#x27;)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># open(&#x27;aa.txt&#x27;,&#x27;w&#x27;)</span></span><br><span class="line"><span class="comment"># 删除文件</span></span><br><span class="line"><span class="comment"># os.remove(&#x27;aa.txt&#x27;)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># os.rename(&#x27;旧名字&#x27;,&#x27;新名字&#x27;) 可以对一个文件进行重命名，也可以用来移动一个文件</span></span><br><span class="line"><span class="comment"># os.rename(&#x27;aa.txt&#x27;,&#x27;bb.txt&#x27;)</span></span><br><span class="line">os.rename(<span class="string">&#x27;bb.txt&#x27;</span>,<span class="string">&#x27;c:/users/lilichao/desktop/bb.txt&#x27;</span>)</span><br><span class="line"></span><br><span class="line">pprint(r)</span><br></pre></td></tr></table></figure></li>
</ol>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Cai XianQuan</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://xquan123.gitee.io/2020/07/19/Python%E5%9F%BA%E7%A1%80/">https://xquan123.gitee.io/2020/07/19/Python%E5%9F%BA%E7%A1%80/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://xquan123.gitee.io" target="_blank">Cquang博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/python/">python</a><a class="post-meta__tags" href="/blog/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6%E8%AF%BE%E7%A8%8B/">计算机科学课程</a></div><div class="post_share"><div class="social-share" data-image="/blog/2020/07/19/Python%E5%9F%BA%E7%A1%80/Python%E5%9F%BA%E7%A1%80%E9%A6%96%E9%A1%B5.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2020/08/08/%E5%B0%9A%E7%A1%85%E8%B0%B7%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8BSpark%E5%9F%BA%E7%A1%80%E8%A7%A3%E6%9E%90-%E4%B8%80/"><img class="prev-cover" src="/blog/2020/08/08/%E5%B0%9A%E7%A1%85%E8%B0%B7%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8BSpark%E5%9F%BA%E7%A1%80%E8%A7%A3%E6%9E%90-%E4%B8%80/%E5%B0%9A%E7%A1%85%E8%B0%B7%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8BSpark%E5%9F%BA%E7%A1%80%E8%A7%A3%E6%9E%90%E9%A6%96%E9%A1%B5.jpg" onerror="onerror=null;src='/blog/img/404.jpg'"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">尚硅谷大数据之Spark基础解析(一)</div></div></a></div><div class="next-post pull-right"><a href="/blog/2020/07/03/hexo-nginx%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E9%83%A8%E7%BD%B2%E5%8D%9A%E5%AE%A2-%E4%BA%8C/"><img class="next-cover" src="/blog/2020/07/03/hexo-nginx%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E9%83%A8%E7%BD%B2%E5%8D%9A%E5%AE%A2-%E4%BA%8C/hexo-nginx%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E9%83%A8%E7%BD%B2%E5%8D%9A%E5%AE%A2-%E4%BA%8C%E9%A6%96%E9%A1%B5.jpg" onerror="onerror=null;src='/blog/img/404.jpg'"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">hexo+nginx服务器上部署博客(二)</div></div></a></div></nav></div><div class="aside_content" id="aside_content"><div class="card-widget card-info"><div class="card-content"><div class="card-info-avatar is-center"><img class="avatar-img" src="/blog/img/%E5%A4%B4%E5%83%8F.gif" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Cai XianQuan</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">22</div></a></div><div class="card-info-data-item is-center"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">33</div></a></div><div class="card-info-data-item is-center"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div><a class="button--animated" id="card-info-btn" href="javascript:;"><i class="fab fa-bookmark"></i><span>加入书签</span></a></div></div><div class="card-widget card-announcement"><div class="card-content"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">一花一世界,一木一菩提╮(￣▽ ￣)╭</div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="card-content"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E7%AB%A0-%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">第一章 前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%BA%8C%E7%AB%A0-%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86"><span class="toc-number">2.</span> <span class="toc-text">第二章 计算机基础知识</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A1%E7%AE%97%E6%9C%BA%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-number">2.1.</span> <span class="toc-text">计算机是什么</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%BB%84%E6%88%90"><span class="toc-number">2.2.</span> <span class="toc-text">计算机的组成</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F"><span class="toc-number">2.3.</span> <span class="toc-text">计算机的使用方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#windows%E7%9A%84%E5%91%BD%E4%BB%A4%E8%A1%8C"><span class="toc-number">2.4.</span> <span class="toc-text">windows的命令行</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%EF%BC%88environment-variable%EF%BC%89"><span class="toc-number">2.5.</span> <span class="toc-text">环境变量（environment variable）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#path%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="toc-number">2.6.</span> <span class="toc-text">path环境变量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%9B%E5%88%B6"><span class="toc-number">2.7.</span> <span class="toc-text">进制</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2"><span class="toc-number">2.8.</span> <span class="toc-text">进制转换</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%95%B0%E8%BD%AC%E6%8D%A2%E6%88%90%E5%8D%81%E8%BF%9B%E5%88%B6%E6%95%B0"><span class="toc-number">2.8.1.</span> <span class="toc-text">二进制数转换成十进制数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E3%80%81%E5%8D%81%E8%BF%9B%E5%88%B6%E6%95%B0%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%95%B0"><span class="toc-number">2.8.2.</span> <span class="toc-text">、十进制数转换为二进制数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%9C%AC%E6%96%87%E4%BB%B6%E5%92%8C%E5%AD%97%E7%AC%A6%E9%9B%86"><span class="toc-number">2.9.</span> <span class="toc-text">文本文件和字符集</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Sublime-Text-3"><span class="toc-number">2.10.</span> <span class="toc-text">Sublime Text 3</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B8%89%E7%AB%A0-Python%E5%85%A5%E9%97%A8"><span class="toc-number">3.</span> <span class="toc-text">第三章 Python入门</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80"><span class="toc-number">3.1.</span> <span class="toc-text">什么是计算机语言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E8%AF%91%E5%9E%8B%E8%AF%AD%E8%A8%80%E5%92%8C%E8%A7%A3%E9%87%8A%E5%9E%8B%E8%AF%AD%E8%A8%80"><span class="toc-number">3.2.</span> <span class="toc-text">编译型语言和解释型语言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Python%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="toc-number">3.3.</span> <span class="toc-text">Python的介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Python%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA"><span class="toc-number">3.4.</span> <span class="toc-text">Python开发环境搭建</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Python%E7%9A%84%E4%BA%A4%E4%BA%92%E7%95%8C%E9%9D%A2"><span class="toc-number">3.5.</span> <span class="toc-text">Python的交互界面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Python%E5%92%8CSublime%E7%9A%84%E6%95%B4%E5%90%88"><span class="toc-number">3.6.</span> <span class="toc-text">Python和Sublime的整合</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-number">3.7.</span> <span class="toc-text">基本语法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="toc-number">3.8.</span> <span class="toc-text">常用操作符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%99%A4%E6%B3%95"><span class="toc-number">3.8.1.</span> <span class="toc-text">除法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B9%82%E8%BF%90%E7%AE%97%EF%BC%9A"><span class="toc-number">3.8.2.</span> <span class="toc-text">幂运算：**</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#not-%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">3.8.3.</span> <span class="toc-text">not 逻辑运算符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B%E6%B3%A8%E6%84%8F%E7%82%B9"><span class="toc-number">3.8.4.</span> <span class="toc-text">一些注意点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%8F%8D%E6%96%9C%E6%9D%A0"><span class="toc-number">3.8.5.</span> <span class="toc-text">使用反斜杠</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E9%87%8D%E5%BC%95%E5%8F%B7"><span class="toc-number">3.8.6.</span> <span class="toc-text">三重引号</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%EF%BC%88object%EF%BC%89"><span class="toc-number">3.9.</span> <span class="toc-text">对象（object）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%BB%93%E6%9E%84"><span class="toc-number">3.10.</span> <span class="toc-text">对象的结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%AF%E5%8F%98%E5%AF%B9%E8%B1%A1%E4%B8%8E%E4%B8%8D%E5%8F%AF%E5%8F%98%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.11.</span> <span class="toc-text">可变对象与不可变对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%AF%E5%8F%98%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.11.1.</span> <span class="toc-text">可变对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%8E%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E8%A7%92%E5%BA%A6%E7%90%86%E8%A7%A3"><span class="toc-number">3.11.2.</span> <span class="toc-text">从数据类型角度理解</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E5%92%8C%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.12.</span> <span class="toc-text">变量和对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">3.13.</span> <span class="toc-text">类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#int-%E5%92%8Cfloat"><span class="toc-number">3.13.1.</span> <span class="toc-text">int()和float()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#str-%E5%92%8Cbool"><span class="toc-number">3.13.2.</span> <span class="toc-text">str()和bool()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BE%8B%E5%AD%90%E8%AF%B4%E6%98%8E"><span class="toc-number">3.13.3.</span> <span class="toc-text">例子说明</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E5%9B%9B%E7%AB%A0-%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="toc-number">4.</span> <span class="toc-text">第四章 流程控制语句</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9D%A1%E4%BB%B6%E5%88%86%E6%94%AF"><span class="toc-number">4.1.</span> <span class="toc-text">条件分支</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF"><span class="toc-number">4.2.</span> <span class="toc-text">循环</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#while%E5%BE%AA%E7%8E%AF"><span class="toc-number">4.2.1.</span> <span class="toc-text">while循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#for%E5%BE%AA%E7%8E%AF"><span class="toc-number">4.2.2.</span> <span class="toc-text">for循环</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9D%A1%E4%BB%B6%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%88%E4%B8%89%E5%85%83%E6%93%8D%E4%BD%9C%E7%AC%A6%EF%BC%89"><span class="toc-number">4.3.</span> <span class="toc-text">条件表达式（三元操作符）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#break%E5%92%8Ccontinue"><span class="toc-number">4.4.</span> <span class="toc-text">break和continue</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#assert-%EF%BC%9A%E6%96%AD%E8%A8%80"><span class="toc-number">4.5.</span> <span class="toc-text">assert ：断言</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%BA%94%E7%AB%A0-%E5%BA%8F%E5%88%97"><span class="toc-number">5.</span> <span class="toc-text">第五章 序列</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%97%E8%A1%A8-%E4%B8%80%E4%B8%AA%E6%89%93%E4%BA%86%E6%BF%80%E7%B4%A0%E7%9A%84%E6%95%B0%E7%BB%84"><span class="toc-number">5.1.</span> <span class="toc-text">列表:一个打了激素的数组</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%97%E8%A1%A8%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-number">5.1.1.</span> <span class="toc-text">列表的方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8"><span class="toc-number">5.1.2.</span> <span class="toc-text">遍历列表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%97%E8%A1%A8%E5%88%87%E7%89%87%EF%BC%88slice%EF%BC%89"><span class="toc-number">5.1.3.</span> <span class="toc-text">列表切片（slice）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0%E5%92%8C%E4%BF%AE%E6%94%B9%E5%85%83%E7%B4%A0"><span class="toc-number">5.1.4.</span> <span class="toc-text">删除元素和修改元素</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%9A%E7%94%A8%E6%93%8D%E4%BD%9C-%E5%BA%8F%E5%88%97"><span class="toc-number">5.2.</span> <span class="toc-text">通用操作(序列)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%92%8C"><span class="toc-number">5.2.1.</span> <span class="toc-text">+ 和 *</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#in-%E5%92%8C-not-in"><span class="toc-number">5.2.2.</span> <span class="toc-text">in 和 not in</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E3%80%81-%E3%80%81is%E3%80%81is-not"><span class="toc-number">5.2.3.</span> <span class="toc-text">&#x3D;&#x3D;、!&#x3D;、is、is not</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#len-%E3%80%81min-%E5%92%8Cmax"><span class="toc-number">5.2.4.</span> <span class="toc-text">len()、min()和max()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B%E5%B8%B8%E8%A7%81%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-number">5.2.5.</span> <span class="toc-text">一些常见的方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#range"><span class="toc-number">5.2.6.</span> <span class="toc-text">range()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BA%8F%E5%88%97%EF%BC%88sequence%EF%BC%89"><span class="toc-number">5.3.</span> <span class="toc-text">序列（sequence）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%83%E7%BB%84%EF%BC%9A%E6%88%B4%E4%B8%8A%E4%BA%86%E6%9E%B7%E9%94%81%E7%9A%84%E5%88%97%E8%A1%A8"><span class="toc-number">5.4.</span> <span class="toc-text">元组：戴上了枷锁的列表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">5.5.</span> <span class="toc-text">字符串</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">5.5.1.</span> <span class="toc-text">格式化字符串</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%8D%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">5.5.2.</span> <span class="toc-text">复制字符串</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E5%85%B8%E5%BD%93%E7%B4%A2%E5%BC%95%E4%B8%8D%E5%A5%BD%E7%94%A8%E6%97%B6%EF%BC%88%E6%98%A0%E5%B0%84%E7%B1%BB%E5%9E%8B%EF%BC%89"><span class="toc-number">5.6.</span> <span class="toc-text">字典当索引不好用时（映射类型）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%AD%97%E5%85%B8"><span class="toc-number">5.6.1.</span> <span class="toc-text">创建字典</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%AD%97%E5%85%B8"><span class="toc-number">5.6.2.</span> <span class="toc-text">使用字典</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9B%86%E5%90%88"><span class="toc-number">5.7.</span> <span class="toc-text">集合</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E9%9B%86%E5%90%88"><span class="toc-number">5.7.1.</span> <span class="toc-text">使用集合</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9B%86%E5%90%88%E7%9A%84%E8%BF%90%E7%AE%97"><span class="toc-number">5.7.2.</span> <span class="toc-text">集合的运算</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E5%85%AD%E7%AB%A0-%E5%87%BD%E6%95%B0"><span class="toc-number">6.</span> <span class="toc-text">第六章 函数</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%AE%80%E4%BB%8B%EF%BC%88function%EF%BC%89"><span class="toc-number">6.1.</span> <span class="toc-text">函数简介（function）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">6.2.</span> <span class="toc-text">函数的参数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8D%E5%AE%9A%E9%95%BF%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">6.3.</span> <span class="toc-text">不定长的参数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-number">6.4.</span> <span class="toc-text">返回值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%A1%A3%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">6.5.</span> <span class="toc-text">文档字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F%E4%B8%8E%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4"><span class="toc-number">6.6.</span> <span class="toc-text">作用域与命名空间</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">6.6.1.</span> <span class="toc-text">作用域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4"><span class="toc-number">6.6.2.</span> <span class="toc-text">命名空间</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%92%E5%BD%92"><span class="toc-number">6.7.</span> <span class="toc-text">递归</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B8%83%E7%AB%A0-%E9%AB%98%E9%98%B6%E5%87%BD%E6%95%B0"><span class="toc-number">7.</span> <span class="toc-text">第七章 高阶函数</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%AB%98%E9%98%B6%E5%87%BD%E6%95%B0"><span class="toc-number">7.1.</span> <span class="toc-text">高阶函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%97%AD%E5%8C%85"><span class="toc-number">7.2.</span> <span class="toc-text">闭包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%A3%85%E9%A5%B0%E5%99%A8"><span class="toc-number">7.3.</span> <span class="toc-text">装饰器</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E5%85%AB%E7%AB%A0-%E5%AF%B9%E8%B1%A1%EF%BC%88Object%EF%BC%89"><span class="toc-number">8.</span> <span class="toc-text">第八章 对象（Object）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%AF%B9%E8%B1%A1%EF%BC%9F"><span class="toc-number">8.1.</span> <span class="toc-text">什么是对象？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB-class"><span class="toc-number">8.2.</span> <span class="toc-text">类(class)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8"><span class="toc-number">8.2.1.</span> <span class="toc-text">类的简单使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-number">8.2.2.</span> <span class="toc-text">类的定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%89%B9%E6%AE%8A%E6%96%B9%E6%B3%95"><span class="toc-number">8.2.3.</span> <span class="toc-text">特殊方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B0%81%E8%A3%85"><span class="toc-number">8.3.</span> <span class="toc-text">封装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%A7%E6%89%BF"><span class="toc-number">8.4.</span> <span class="toc-text">继承</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%9A%E6%80%81"><span class="toc-number">8.5.</span> <span class="toc-text">多态</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E4%B8%AD%E7%9A%84%E5%B1%9E%E6%80%A7%E5%92%8C%E6%96%B9%E6%B3%95"><span class="toc-number">8.6.</span> <span class="toc-text">类中的属性和方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6"><span class="toc-number">8.7.</span> <span class="toc-text">垃圾回收</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%89%B9%E6%AE%8A%E6%96%B9%E6%B3%95-1"><span class="toc-number">8.8.</span> <span class="toc-text">特殊方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#str"><span class="toc-number">8.8.1.</span> <span class="toc-text">__str__()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#repr"><span class="toc-number">8.8.2.</span> <span class="toc-text">_\repr__()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E7%89%B9%E6%AE%8A%E6%96%B9%E6%B3%95"><span class="toc-number">8.8.3.</span> <span class="toc-text">其他特殊方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A8%A1%E5%9D%97%EF%BC%88module%EF%BC%89"><span class="toc-number">8.9.</span> <span class="toc-text">模块（module）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8C%85%EF%BC%88package%EF%BC%89"><span class="toc-number">8.10.</span> <span class="toc-text">包（package）</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B9%9D%E7%AB%A0-%E5%BC%82%E5%B8%B8%E5%92%8C%E6%96%87%E4%BB%B6"><span class="toc-number">9.</span> <span class="toc-text">第九章 异常和文件</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8"><span class="toc-number">9.1.</span> <span class="toc-text">异常</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8%E8%AF%B4%E6%98%8E"><span class="toc-number">9.1.1.</span> <span class="toc-text">异常说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%84%E7%90%86%E5%BC%82%E5%B8%B8"><span class="toc-number">9.1.2.</span> <span class="toc-text">处理异常</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8%E7%9A%84%E4%BC%A0%E6%92%AD%EF%BC%88%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8%EF%BC%89"><span class="toc-number">9.1.3.</span> <span class="toc-text">异常的传播（抛出异常）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8"><span class="toc-number">9.1.4.</span> <span class="toc-text">抛出异常</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%EF%BC%88File%EF%BC%89"><span class="toc-number">9.2.</span> <span class="toc-text">文件（File）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%93%E5%BC%80%E6%96%87%E4%BB%B6%EF%BC%88open%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-number">9.2.1.</span> <span class="toc-text">打开文件（open函数）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B3%E9%97%AD%E6%96%87%E4%BB%B6%EF%BC%88close%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-number">9.2.2.</span> <span class="toc-text">关闭文件（close函数）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E8%AF%BB%E5%8F%96"><span class="toc-number">9.2.3.</span> <span class="toc-text">文件读取</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AF%BB%E5%8F%96%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%96%87%E4%BB%B6"><span class="toc-number">9.2.3.1.</span> <span class="toc-text">读取二进制文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AF%BB%E5%8F%96%E6%96%87%E4%BB%B6%E7%9A%84%E4%BD%8D%E7%BD%AE%EF%BC%88seek%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-number">9.2.3.2.</span> <span class="toc-text">读取文件的位置（seek函数）</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E5%86%99%E5%85%A5%EF%BC%88write%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-number">9.2.4.</span> <span class="toc-text">文件写入（write函数）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E7%9A%84%E5%85%B6%E4%BB%96%E6%93%8D%E4%BD%9C"><span class="toc-number">9.2.5.</span> <span class="toc-text">文件的其他操作</span></a></li></ol></li></ol></li></ol></div></div></div><div class="card-widget card-recent-post"><div class="card-content"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/01/29/%E6%A1%86%E6%9E%B6%E5%A5%97%E8%B7%AF%E8%A7%A3%E7%AE%97%E6%B3%95/" title="框架套路解算法"><img src="/blog/2021/01/29/%E6%A1%86%E6%9E%B6%E5%A5%97%E8%B7%AF%E8%A7%A3%E7%AE%97%E6%B3%95/%E6%A1%86%E6%9E%B6%E5%A5%97%E8%B7%AF%E8%A7%A3%E7%AE%97%E6%B3%95%E9%A6%96%E9%A1%B5.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="框架套路解算法"/></a><div class="content"><a class="title" href="/blog/2021/01/29/%E6%A1%86%E6%9E%B6%E5%A5%97%E8%B7%AF%E8%A7%A3%E7%AE%97%E6%B3%95/" title="框架套路解算法">框架套路解算法</a><time datetime="2021-01-29T07:37:55.000Z" title="发表于 2021-01-29 15:37:55">2021-01-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/01/07/shell%E8%84%9A%E6%9C%AC%E5%AD%A6%E4%B9%A0/" title="shell脚本学习"><img src="/blog/2021/01/07/shell%E8%84%9A%E6%9C%AC%E5%AD%A6%E4%B9%A0/shell%E8%84%9A%E6%9C%AC%E5%AD%A6%E4%B9%A0%E9%A6%96%E9%A1%B5.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="shell脚本学习"/></a><div class="content"><a class="title" href="/blog/2021/01/07/shell%E8%84%9A%E6%9C%AC%E5%AD%A6%E4%B9%A0/" title="shell脚本学习">shell脚本学习</a><time datetime="2021-01-07T03:29:18.000Z" title="发表于 2021-01-07 11:29:18">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/12/28/Linux%E9%97%AE%E9%A2%98%E9%9B%86%E9%94%A6/" title="Linux问题集锦"><img src="/blog/2020/12/28/Linux%E9%97%AE%E9%A2%98%E9%9B%86%E9%94%A6/Linux%E9%97%AE%E9%A2%98%E9%9B%86%E9%94%A6%E9%A6%96%E9%A1%B5.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Linux问题集锦"/></a><div class="content"><a class="title" href="/blog/2020/12/28/Linux%E9%97%AE%E9%A2%98%E9%9B%86%E9%94%A6/" title="Linux问题集锦">Linux问题集锦</a><time datetime="2020-12-28T01:56:38.000Z" title="发表于 2020-12-28 09:56:38">2020-12-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/12/13/%E5%B8%B8%E7%94%A8%E7%9A%84Linux%E5%91%BD%E4%BB%A4%E9%9B%86/" title="常用的Linux命令集"><img src="/blog/2020/12/13/%E5%B8%B8%E7%94%A8%E7%9A%84Linux%E5%91%BD%E4%BB%A4%E9%9B%86/%E5%B8%B8%E7%94%A8%E7%9A%84Linux%E5%91%BD%E4%BB%A4%E9%9B%86%E9%A6%96%E9%A1%B5.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="常用的Linux命令集"/></a><div class="content"><a class="title" href="/blog/2020/12/13/%E5%B8%B8%E7%94%A8%E7%9A%84Linux%E5%91%BD%E4%BB%A4%E9%9B%86/" title="常用的Linux命令集">常用的Linux命令集</a><time datetime="2020-12-13T14:32:23.000Z" title="发表于 2020-12-13 22:32:23">2020-12-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2020/12/13/Flink%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="Flink入门学习"><img src="/blog/2020/12/13/Flink%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/Flink%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0%E9%A6%96%E9%A1%B5.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Flink入门学习"/></a><div class="content"><a class="title" href="/blog/2020/12/13/Flink%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="Flink入门学习">Flink入门学习</a><time datetime="2020-12-13T10:21:57.000Z" title="发表于 2020-12-13 18:21:57">2020-12-13</time></div></div></div></div></div></div></div></main><footer id="footer" style="background-image: url(/blog/2020/07/19/Python%E5%9F%BA%E7%A1%80/Python%E5%9F%BA%E7%A1%80%E9%A6%96%E9%A1%B5.jpg)"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By Cai XianQuan</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">Hi,  welcome  to  my  <a  target="_blank" rel="noopener" href="https://www.caixianquan.tk">blog</a>!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="/blog/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/blog/js/search/local-search.js"></script><div class="js-pjax"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/js/bookmark.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script></div></body></html>